mercredi 30 octobre 2019

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.



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/2PxexVa
via IFTTT

Aucun commentaire:

Enregistrer un commentaire