laravel validation required if

0
'tipo'              => 'required',
'cpf'               => 'required_if:tipo,F|cpf',
'rg'                => 'required_if:tipo,F',
'cnpj'              => 'required_if:tipo,J|cnpj',
'inscricao_estadual'=> 'required_if:tipo,J',

Next, I am using this required if , and when request comes with type 'J' it validates in cpf, how to solve?     

asked by anonymous 19.02.2018 / 21:48

1 answer

1

required_if can also be used this way:    required_if:<field>,<comparador>,<valor>

Example:

'field1' => 'required|numeric',
'field2' => 'required_if:field1,==,2|string'

In your case, I believe that a condition concatenated string could solve, eg:

Valitador::make($request->all(), [
  'tipo'              => 'required',
  'cpf'               => ($request['tipo']=='F' ? 'required|cpf' : 'nullable'),
  'rg'                => 'required_if:tipo,==,F',
  'cnpj'              => ($request['tipo']=='J' ? 'required|cnpj' : 'nullable'),
  'inscricao_estadual'=> 'required_if:tipo,==,J'
]);
    
05.07.2018 / 20:00