I'm new to Laravel.
Could someone explain why max
validator doesn't work as I expected in this case?
$input = ["schoolSeatsTotal" => '2000'];
$rules = ['schoolSeatsTotal'=>'max:300'];
$validator = Validator::make($input, $rules);
$validator->fails(); //Expected: true, Actual: false.
I know of at least two ways. // option one: 'in' takes a comma-separated list of acceptable values $rules = [ 'field' => 'in:hello', ]; // option two: write a matching regular expression $rules = [ 'field' => 'regex:^hello$', ];
You should add all your validation logic in the passes() function. It should return true or false based on the logic you have written in the function. The message() function returns a string that specifies the error message to be displayed in case the validation fails.
You have schoolSeatsTotal
as a string. For string data, max value corresponds to the number of characters. You want to validate an integer instead.
So change
$input = ["schoolSeatsTotal" => '2000'];
to
$input = ["schoolSeatsTotal" => 2000];
To make sure you are validating numbers - do this:
$rules = ['schoolSeatsTotal'=>'numeric|max:300'];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With