Sunday, February 20, 2022

[FIXED] Laravel Validation Error Message required_if not working

Issue

I am validating a form using Laravel 5.2

using validation check required_if for input youtube-embed. validation takes place successfully but the custom error message does not work. Here is the code maybe some one can take a look.

$messages = [
        'youtube-embed.required_if' => 'Please paste in your youtube embed code',
    ];

$this->validate($request, [
        'youtube-embed'      => 'required_if:youtube,on',
    ]);

This is the error message that laravel is returning instead of my custom error:

The youtube-embed field is required when youtube is on.

Solution

Try this:

$this->validate($request, 
    ['youtube-embed'      => 'required_if:youtube,on',], 
    ['required_if' => 'Please paste in your youtube embed code',]
);

Basically you can pass your custom messages as third parameter in validate function.



Answered By - Can Celik

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.