Laravel validator require

1

When you put the required option in the validation rules of Laravel

$validator=\Validator::make($request->all(),['po_bairro'=>'require|string|min:5|max:50']);

It requires you to have the value to continue the program.

However, in my case, I did not put 'require' as an option. I only set%% of% in the rules.

$validator=\Validator::make($request->all(),['po_bairro'=>'string|min:5|max:50']);

By submitting the form with the empty field ( string|min:5|max:50 ), it gives me the error of the po_bairro rule.

Here comes the question, but I do not require, the validator should not let pass?

    
asked by anonymous 16.03.2018 / 15:45

1 answer

1
  

Here comes the question, but I do not require, the validator should not let pass?

The answer is not because in the documentation itself there is an excerpt that answers your question:

string

The field under validation must be the string. If you would like to allow the field to be null , you should assign the nullable rule to the field.

translation

The field in validation must be a string . If you want to allow the field to also be nulo , you must assign the nullable rule to the field.

With this explanation change your validation code to:

$validator = \Validator::make($request->all(), ['po_bairro' => 'nullable|min:5|max:50']);

References:

16.03.2018 / 16:56