Search code examples
laravel-5laravel-5.5laravel-validationphp-7.1

Laravel 5.5 Validation with OR/ELSE conditional


i'm new to laravel and i would like to know if there are a way to use or/else conditional in $rules, because I am struggling trying to do the following validation: If the field is Z the size must be y, else, x. I tried this, but it always validade data with size:11

'type   => 'required',
'field' => ['required', ('type == 1' ? 'size:11' : 'size:14')],

Anyone has a clue? thanks in advance


Solution

  • I assume you use Form request validation so you can do something like this:

    $rules = [
       'type' => 'required',
       'field' =>  ['required'];
    ];
    
    $rules['field'][] = ($this->input('type') == 1) ? 'size:11' : 'size:14';
    
    return $rules;