Laravel 5.4 - How to use multiple error messages for the same custom validation rule

后端 未结 4 1772
失恋的感觉
失恋的感觉 2021-02-19 21:40

In order to reuse code, I created my own validator rule in a file named ValidatorServiceProvider :

class ValidatorServiceProvider extends Servic         


        
4条回答
  •  心在旅途
    2021-02-19 22:41

    Unfortunately Laravel doesn't currently provide a concrete way to add and call your validation rule directly from your attribute params array. But that's does not exclude a potential and friendly solution based on Trait and Request usage.

    Please find below my solution for example.

    First thing is to wait for the form to be processed to handle the form request ourselve with an abstract class. What you need to do is to get the current Validator instance and prevent it from doing further validations if there's any relevant error. Otherwise, you'll store the validator instance and call your custom user validation rule function that you'll create later :

    after(function ($validator) {
                if ($validator->errors()->all()) {
                    // Stop doing further validations
                    return;
                }
                $this->v = $validator;
                $this->next();
            });
        }
    
        /**
         * Add custom post-validation rules
         */
        protected function next()
        {
    
        }
    }
    

    The next step is to create your Trait which will provide the way to validate your inputs thanks to the current validator instance and handle the correct error message you want to display :

    input($emailField);
    
            $user = \App\User::where('email', $email)->first();
    
            if (! $user) {
                return $this->v->errors()->add($emailField, 'Email not found');
            }
            if (! $user->valid_email) {
                return $this->v->errors()->add($emailField, 'Email not valid');
            }
    
            // MORE VALIDATION POSSIBLE HERE
            // YOU CAN ADD AS MORE AS YOU WANT
            // ...
        }
    }
    

    Finally, do not forget to extend your MyCustomFormRequest. For example, after your php artisan make:request CreateUserRequest, here's the easy way to do so :

    validateUserEmailValidity('email');
        }
    
        /**
         * Determine if the user is authorized to make this request.
         *
         * @return bool
         */
        public function authorize()
        {
            return true;
        }
    
        /**
         * Get the validation rules that apply to the request.
         *
         * @return array
         */
        public function rules()
        {
            return [
                'email' => 'bail|required|email|max:255|unique:users',
                'password' => 'bail|required',
                'name' => 'bail|required|max:255',
                'first_name' => 'bail|required|max:255',
            ];
        }
    }
    

    I hope that you'll find your way in what I suggest.

提交回复
热议问题