A
A
Arthur2018-11-12 09:10:22
Laravel
Arthur, 2018-11-12 09:10:22

Depending on the request, different form request validation conditions?

There is a validation done through a form request. The task is to change the validation parameters depending on the value of one of the parameters. The example below, everything works, but when there are several of these parameters, it turns out you need to repeat it.
I see a solution like this, create an empty array and fill it when the parameters match and return an associative array at the end. Is there a better solution?

if ($this->get('parametr') == 1) {
                return [
                    'name' => 'required'
                ];
            } else {
                return [
                    'name' => 'required',
                    'age' => 'required'
                ];
            }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
netrox, 2018-11-12
@ART_CORP

blog.mauriziobonani.com/conditional-validation-on-...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question