D
D
Denis2020-02-23 21:00:40
Laravel
Denis, 2020-02-23 21:00:40

Field validation with Laravel default value?

Just started getting to know Laravel. Tell me how to do it right in such a situation?
We fill out the form when adding an article to the database, check the fields,

$this->validate($request, [
            'name' => 'required|unique:articles',
            'body' => 'required|min:100',
            'state' => ''
        ]);

How to correctly check the state field if it has a default value? Or is it better to remove it from the form altogether in this case and not to bathe?

Looked in docks Available Validation Rules But if it is fair more came to that it is better to remove it from the form. How will it be right?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
J
JhaoDa, 2020-02-23
@JhaoDa

Why should it come from the client if it's "default"? Obviously, if there is no field or it is empty, then the application should set the default value, i.e. This does not apply to validation.

Y
Yuri Kulaxyz, 2020-02-23
@Kulaxyz

If this state field is not set by the user, then remove it from the form request. If by "default" you mean that if the user leaves it blank, then the default value will be set, then in the rules, validate it like everyone else, just without required.

K
Konstantin B., 2020-02-23
@Kostik_1993

What does Laravel have to do with it? This is a matter of your competence, not a specific tool. You write at least on Laravel, even on Django, it doesn’t matter, the question is your knowledge

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question