Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel max Validator not working

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. 
like image 835
randomor Avatar asked May 29 '14 16:05

randomor


People also ask

How do you validate exact words in laravel?

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$', ];

How do you add a validation rule in laravel?

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.


1 Answers

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'];
like image 70
Laurence Avatar answered Sep 22 '22 06:09

Laurence