Laravel Validator require

Asked

Viewed 328 times

1

When placing the option required 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 just set string|min:5|max:50 in the rules.

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

When sending the form with the empty field (po_bairro), he gives me the error of the rule min.

Here comes the question, but I did not set require, the validator should not let go?

  • Dump on $request->input('po_bairro') to see how it arrives. When you send it empty. I am considering and trying to understand that the "" is entering as string, then it falls into the rule.

  • Test one array_filter($request->all()). So Validator really had received nothing from that field.

1 answer

1


Here comes the question, but I did not set require, the validator should not let go?

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

string

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

translating

The field under validation shall be a string. If you want to allow the field to be nulo, you must assign the rule nullable to the countryside.


By this explanation change your validation code to:

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

References:

  • 1

    Perfect, thank you very much!!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.