0

I have server validation below:

$this->validate([
    'first_name' => ['required','min:2','max:30',new PersonNameRule],
    'last_name' => ['required','min:2','max:30',new PersonNameRule],
    'username' => ['required','confirmed',new UsernameRule],
    'password' => 'required|confirmed|min:6|max:20',
    'birthdate' => ['required','date',new EligibleAgeRule(21)]
]);

In front-end, I intentionally fail the validation and console.log() the errors. The log returned looks like below: enter image description here

Now my question is, where is the message (The given data was invalid) came from? Can I customize it?

3
  • Better Use custom Request For these type of validation Commented Apr 29, 2020 at 10:44
  • If you want to do like this make sure you accepting 'application/json' Commented Apr 29, 2020 at 10:52
  • First U must check what rules failed, then -laravel.com/docs/6.x/validation#custom-error-messages Commented Apr 29, 2020 at 10:54

1 Answer 1

1

I don't know about your objective but if your finding the file. Its in the directory ..\Illuminate\Validation\ValidationException.php.

You can edit the message you mention inside the __construct method. See example below:

public function __construct($validator, $response = null, $errorBag = 'default') {
    parent::__construct('The given data was invalid.'); //change the message here

    $this->response = $response;
    $this->errorBag = $errorBag;
    $this->validator = $validator;
}
Sign up to request clarification or add additional context in comments.

1 Comment

thanks it works! I would like to do some stuffs here that's why I ask.

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.