t0ledoj
t0ledoj

Reputation: 33

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

Upvotes: 0

Views: 2578

Answers (2)

Marcin Nabiałek
Marcin Nabiałek

Reputation: 111829

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;

Upvotes: 1

Vicky Gill
Vicky Gill

Reputation: 734

Use Laravel Validator , Refer Link: https://laravel.com/docs/5.5/validation

 $validatedData = $request->validate([
            'title' => 'required|unique:posts|max:255',
            'body' => 'required',
        ]);

Upvotes: 0

Related Questions