natral
natral

Reputation: 1098

Custom error message for `requiredIf` validation in laravel

I'm working on a Laravel 5.8 project and trying to show custom validation messages for a validation which uses the requiredIf validation rule.

Here is how I have it set up:

$validation = Validator::make(
    $request->all(),
    [
        ...
        'sum' => [
            Rule::requiredIf(function() use ($request){
                $model = Model::find($request->id);
                return $model->is_special; //returns a boolean value
            }),
            'numeric'
        ],
        ...
    ],
    [
        ...
        'sum.required_if' => 'This cannot be blank',
        'sum.numeric' => 'Must use a number here',
        ...
    ]
);

Now the validation is working correctly and the custom message for the numeric validation shows as should, but the message I get for the requiredIf() method is Laravel's default error message.

I also tried using 'sum.requiredIf' => '...' but that didn't work either and can't seem to find any documentation or example for this scenario.

Upvotes: 8

Views: 7312

Answers (3)

scridget
scridget

Reputation: 92

Looks like as of Laravel 8, using required_if works as expected, and alternatively will not fall back on required as mentioned previously:

'sum.required_if' => 'This cannot be blank',

Upvotes: 1

natral
natral

Reputation: 1098

I was tinkering with this for a while and noticed that for this to work I needed to define

'sum.required' => 'This cannot be blank'

and not 'sum.required_if' => 'This cannot be blank',.

Not sure if this is expected behavior or just a workaround but my deduction is that with the callback Rule::requiredIf(function() use ($request){...}) the parameters :other and :value are not passed so it falls back onto required messaging and I guess this makes sense since required_if and required would not be used on the same :attribute.

Hope this helps anyone who comes across this problem.

Upvotes: 12

Mahmoud Gad
Mahmoud Gad

Reputation: 338

First, create a rule name isSpecial or whatever

php artisan make:rule isSpecial

Go to App\Rules\isSpecial.php

    private $id;

    public function __construct($id) // pass id or what you need
    {
        //
        $this->id=$id;
    }

    public function passes($attribute, $value) // customize your rules here
    {
        //
        return Model::find($request->id)->is_special;
    }


    public function message() // here is answer for your question
    {
        return 'The validation error message.'; // your message
    }

in your controller

 use App\Rules\isSpecial;

  \Validator::make($request->all(), [
            'sum' => new isSpecial() ,
  ])->validate();

another idea :

Specifying Custom Messages In Language Files In most cases, you will probably specify your custom messages in a language file instead of passing them directly to the Validator. To do so, add your messages to custom array in the resources/lang/xx/validation.php language file.

'custom' => [
    'email' => [
        'required' => 'We need to know your e-mail address!',
    ],
],

Simple notice: - I suggest using HTTP Requests instead use validation in your controller and function direct

Upvotes: 1

Related Questions