Laravel custom validation

Published 5 months ago by codemode

In my controller i'm doing some validation like :

 $this->validate(request(),  
            [ 'add_button' => 'required|integer|notIn:'.$requested_by ], 
            [ 'notIn' => 'This value has to be different' ]);

...this is in reference to the following format : $this->validate($request, $rules, $customMessages);

So, i'm using the "notIn" validator to check that the "add_button" value is not equal to "$requested_by"

Though the validation works, but the custom message is not not displayed. On the contrary, if i put a custom validation message for "required" or something like "max" - then the custom validation message is displayed just fine.

Where am i going wrong?

Thanks.

Best Answer (As Selected By codemode)
bobbybouwmann

I think the rule is incorrect. It should not_in instead of notIn

Documentation: https://laravel.com/docs/5.5/validation#custom-error-messages

bobbybouwmann

I think the rule is incorrect. It should not_in instead of notIn

Documentation: https://laravel.com/docs/5.5/validation#custom-error-messages

codemode

The documentation actually has the typo error. Thanks for pointing out @bobbybouwmann

bobbybouwmann

@codemode The documentation does not have the typo! I think you misunderstood the documentation

return [
    'field' => 'not_in:foo,bar',
];

// VS

return [
    'field' => Rule::notIn(['foo', 'bar']),
];

Multiple ways of achieving the same thing ;)

Please sign in or create an account to participate in this conversation.