Ad

Input Required Only If Specific Option Is Selected

- 1 answer

I have a form that contains a select and an input field. To simplify the example, let's say the three options of the select are 1, 2, and 3.

I need to create a set of validation rules where input is only required if option 2 is selected. How can I create this rule?

Here is my set of rules so far:

$rules = array(
    'selectField' => 'required',
    'inputField' => '', // only required if option 2 is selected
);
Ad

Answer

You could use the required_if validation rule?

https://laravel.com/docs/5.1/validation#rule-required-if

$rules = [
    'selectField' => 'required',
    'inputField' => 'required_if:selectField,2',
];
Ad
source: stackoverflow.com
Ad