Search code examples
phplaravelvalidationlaravel-8

Give name to custom validation Rule - Laravel 8


Laravel 8 makes it possible to create custom Validation rules: https://laravel.com/docs/8.x/validation#custom-validation-rules

php artisan make:rule Euro

But then you have to pass the rule as a object (instead of a string):

new Euro

instead of the regular string notation

'required|euro'

Is there anyway to "register" the new Rule classes to a string identifier and use them like you can you the pre-existing rules?


Solution

  • You can do it the following way.

    Create two additional methods in your rule, besides the default ones (passes(), message()):

    • handle() -- This one will return your rule's string handle. It's sole purpose is to keep track of that handle in a single place.
    • validate() -- That's the one which will be invoked by the validator. This should essentially be just a pass through to passes() so you can keep your validation logic in one place. Additionally you should pass the message into the validator. In most cases you also want to keep the message logic in one place.
    
    use Illuminate\Validation\Validator;
    
    public static function handle(): string
    {
        return 'euro';
    }
    
    
    public function validate(string $attribute, $value, $params, Validator $validator): bool
    {
        $handle = $this->handle();
    
    
        $validator->setCustomMessages([
            $handle => $this->message(),
        ]);
    
        return $this->passes($attribute, $value);
    }
    

    Register your class using the Validator facade or the factory in a Service Provider's boot() method:

    
    namespace App\Providers;
    
    use App\Rules\Euro;
    use Illuminate\Contracts\Validation\Factory as ValidatorFactory;
    use Illuminate\Support\ServiceProvider;
    
    class ValidationServiceProvider extends ServiceProvider
    {
        /**
         * Register services.
         *
         * @return void
         */
        public function register()
        {
            //
        }
    
        /**
         * Bootstrap services.
         *
         * @return void
         */
        public function boot(ValidatorFactory $validator)
        {
            $validator->extend(Euro::handle(), Euro::class);
        }
    }
    

    That's it. Don't forget to register your service provider if you created a dedicated one.