How to validate at least one mandatory field?

Asked

Viewed 714 times

3

I have two CPF and CNPJ fields, the user must type only one, but never leave the two fields empty. This way it makes mandatory the two fields:

 [
   'Cpf' => array('required'),
   'Cnpj' => array('required'),
 ]

Is there any way in Laravel to validate only one required field when one is completed?

  • 1

    What version of Laravel ?

  • Try it this way: 'cpf' => 'required|required_without_all:cnpj', 'cnpj' => 'required|required_without_all:cpf',

  • @Zooboomafoo VERSION = '5.1.45 (LTS)';

  • @Zooboomafoo I think it doesn’t work for this version: Method [validateRequired|requiredWithoutAll] does not exist.

1 answer

3


One can use the required_without or required_without_all.

[
    'Cpf'  => 'required_without:Cnpj',
    'Cnpj' => 'required_without:Cpf',
]
  • 1

    Thanks, it worked with the required_without

  • Fancy ten! Documentation...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.