1

I'm trying to use the Laravel validator to include some custom error messages before I run the validate() method. However it appears that running this method then removed any previously added errors.

I can confirm that the error message appears when I dump out the validator messages before hitting validate()

    $validator = Validator::make(
        $this->data,
        $this->rules
    );

    $validator->errors()->add('meal', 'The meal field is required.');

    $validator->validate();

How I can validate my data but still include the error relating to the meal?

lky
  • 1,081
  • 3
  • 15
  • 31

1 Answers1

1

I believe what you want to use is the after validation hook. This will allow you to add more errors like so:

$validator = Validator::make(
    $this->data, 
    $this->rules
);

$validator->after(function ($validator) {
    $validator->errors()->add(
        'field', 'Something is wrong with this field!'
    );
});
 
$validator->validate();
Jonathan
  • 211
  • 2
  • 4