0
votes

I am using Laravel 5.3 and I need to validate a field

The field I need to validate is only required if another field has the value 1 and this works.

If the field is required, the value entered must be in a given array.

The problem is the value being validated is NULL so doesn't appear in the array and validation fails

Here is my rule so far

$test->other_field = 0
$test->my_field = NULL;

$rules = array(
    'my_field' => 'required_if:other_field,1', //|in:' . implode(',', $array),
);

The required_if works fine but if i uncomment the in part, the validation fails

1

1 Answers

0
votes

I solve it by creating a new FormRequest and adding the following code.

public function rules(Request $request)
{
    if($request->has('other_field '))
    {
        $rules = array(
            'my_field' => 'in:' . implode(',', $array)
        );

        return $rules;
    }
}