juhlinus/kakunin

Fund package maintenance!
juhlinus

v2.0.0-beta1 2021-10-01 21:14 UTC

This package is auto-updated.

Last update: 2024-11-29 05:51:34 UTC


README

πŸ’° Is this useful to you?

Consider sponsoring me on github! πŸ™

πŸ’Ύ Installation

composer require juhlinus/kakunin

πŸ€” Usage

Kakunin relies on Custom Form Requests.

Add the ValidatesInertiaInput trait to your newly generated form request like so:

<?php

namespace App\Http\Requests;

use Kakunin\Concerns\ValidatesInertiaInput;
use Illuminate\Foundation\Http\FormRequest;

class CustomFormRequest extends FormRequest
{
    use ValidatesInertiaInput;

    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return false;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            //
        ];
    }
}

In order for the validation to be instant you need to make use of a watcher.

Here's an example in Vue.js:

<template>
    <div>
        <input v-model="email">
    </div>
</template>

<script>
    export default {
        watch: {
            email: function (email) {
                this.$inertia.post('/users', {
                    email: email,
                    validate: true,
                });
            }
        }
    }
</script>

Note that I'm passing a validate parameter. If this isn't passed then Kakunin will not validate your request.

That's it! Happy validating!

πŸ“ Configuration

If you wish to change the validate to something else, then you can add KAKUNIN_VALIDATION_KEY to your .env file. Lastly, add the following to your config/services.php file:

'kakunin' => [
    'validation_key' => env('KAKUNIN_VALIDATION_KEY'),
],

β›© That's a stupid name for a package

Kakunin(η’Ίθͺ) is the Japanese verb "to validate".