Skip to content
Advertisement

Required If validation for Laravel array with empty value

I have the following input

JavaScript

and have a Request class with following rules :

JavaScript

But it is showing the following validation error if I give the above input, which has admin as true.

JavaScript

How can I fix this. I need to validate the environment_roles.*.role_id and environment_roles.*.environment_id when the value for admin is true.

Advertisement

Answer

If you are always sending the admin prop it would be more suitable to make it nullable not required. You could try with that:

JavaScript

But your error shows that the role and the environment id’s does not exist in the database ( The exists rule ). Setting those two fields to nullable means it will not trigger the exists rule.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement