T
T
Twelfth Doctor2017-08-11 17:42:57
Laravel
Twelfth Doctor, 2017-08-11 17:42:57

How to apply different validation rules depending on the value of the form select?

Hello.
How can I apply different validation rules to a form field depending on the value of another form field?

<form method = "post">
<!--В зависимости от значения этого селекта применяем нужные правила валидации-->
<select name = "validate">
<option value = "email">E-mail</option>
<option value = "numeric">Numeric</option>
<option value = "url">URL</option>
</select>
<!--К этому полю-->
<input name = "content">
<input type = "submit">
</form>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
T
Twelfth Doctor, 2017-08-11
@verdex

Something like this

public function rules()
    {
        switch (\Request::get('validate')) {
            case 'email':
                return [
                    'content' => 'email|required'
                ];
                break;
            case 'url':
                return [
                    'content'=>'url|required'
                ];
                break;
case 'numeric':
return [
'content'=>'numeric|required'
];
break;
        }
    }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question