I want to give a validation rule for card information:
required
whennumber_of_guests
field value is greater than or equal to15
card_information
will berequired
whennumber_of_guests
field value is greater than or equal to15
$rules = [
'number_of_guests' => 'numeric|required|max:100',
'card_information' => 'required_if:number_of_guests,>=15'
];
3
Answers
The required_if rule needs a slight adjustment to work with numerical values. You can use the integer rule instead of numeric for the number_of_guests field to ensure it’s an integer, and then use required_if with the appropriate condition.
I’d use something like
The key change is in the card_information rule. The correct syntax for the required_if rule is: