Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[11.x] Prohibited If Declined and Prohibited If Accepted validation rules #54608

Merged

Conversation

osama-98
Copy link
Contributor

prohibited_if_accepted and prohibited_if_declined


Description


The prohibited_if_accepted and prohibited_if_declined validation rules are introduced to simplify the logic of prohibiting a field based on boolean-like values (accepted and declined). These rules are designed to make the validation more intuitive when dealing with specific conditions where fields should be prohibited if another field has the value of "accepted" (e.g., yes, 1, true) or "declined" (e.g., no, 0, false).

Current Prohibited If Validation


Currently, to implement the same functionality with prohibited_if, you would need to explicitly list the possible truthy and falsy values of a boolean field like so:

For Prohibiting When Accepted (Boolean true):

'foo' => 'required',
'bar' => 'prohibited_if:foo,yes,on,1,true',

For Prohibiting When Declined (Boolean false):

'foo' => 'required',
'bar' => 'prohibited_if:foo,no,off,0,false',

While this works, it requires manually specifying all the possible representations of "accepted" and "declined", which can be cumbersome and makes the code harder to read.

Prohibited If Accepted / Declined Validation


With the addition of these two new rules, the validation becomes cleaner and more readable.

prohibited_if_accepted

This rule will prohibit the field if the other field is "accepted" (i.e., has a truthy value, such as yes, 1, true, etc.).

'foo' => 'required',
'bar' => 'prohibited_if_accepted:foo',

prohibited_if_declined

This rule will prohibit the field if the other field is "declined" (i.e., has a falsy value, such as no, 0, false, etc.).

'foo' => 'required',
'bar' => 'prohibited_if_declined:foo',

Conclusion


By introducing prohibited_if_accepted and prohibited_if_declined, we make the validation syntax more intuitive and easier to read, especially when dealing with boolean-like values that represent "acceptance" or "decline". These new rules streamline the logic, avoiding the need to list out all possible representations of true or false.

These rules were inspired by the existing required_if_accepted and required_if_declined validation rules, which similarly simplify handling acceptance and declination conditions in validation.

This update improves code readability, reduces boilerplate, and provides a clearer way to enforce validation based on the acceptance or declination of a related field.

@taylorotwell taylorotwell merged commit 17786ca into laravel:11.x Feb 14, 2025
22 of 44 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants