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?

2
  • Does this answer your question? Custom Laravel validation messages Commented May 4, 2022 at 10:44
  • Thanks but unfortunetly that isn't quite what I'm looking for. Commented May 4, 2022 at 10:50

1 Answer 1

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();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.