2
votes

I'm validating a form and if a checkbox is_external_user has value of 1, I want to make the email field required and validate the type email.

Here's my validation rule:

 'email' => [
    'validation'    => 
      'required_if:Users.is_external_user,1|email',
 ],

required_if is working as expected, but type email validation returns the error that it is not a valid email address without being required.

Is there working way to use both require_if and email rules?

1
You could always take a look at conditionally adding rules.milo526

1 Answers

4
votes

I found a decision:

'email' => [
    'validation'   => 'sometimes|nullable|required_if:Users.is_external_user,1|email',
]

I've added sometimes|nullable to the rules.