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

[8.x] Add prohibited_if and prohibited_unless validation rules #36516

Merged
merged 1 commit into from
Mar 9, 2021
Merged

[8.x] Add prohibited_if and prohibited_unless validation rules #36516

merged 1 commit into from
Mar 9, 2021

Conversation

jessarcher
Copy link
Member

I've found myself needing these validation rules on a few occasions, where exclude_if and exclude_unless don't quite go far enough.

For example, in the project I'm currently working on, the following payload includes contradictory information that is not sufficient to just exclude. Instead we need an outright validation failure.

Validator::validate([
    'is_deceased' => false, 
    'date_of_death' => '2021-03-09'
], [
    'date_of_death' => 'prohibited_unless:is_deceased,true'
]);

Not sure if this is a common enough use-case to go into the framework, but hopefully it helps others 🙂

If this gets merged, I will follow up with a PR to laravel/docs and the language file in laravel/laravel.

@rodrigopedra
Copy link
Contributor

I know both can be considered synonyms, but I find forbidden easier to remember.

Might be because it is more common on web-development jargon, and I am not a native English speaker.

Besides of that, nice addition, I wrote a custom Rule for a past project for that exact use-case.

@timacdonald
Copy link
Member

timacdonald commented Mar 9, 2021

I think both words make sense, but my only concern with forbidden is the association with the 403 Forbidden response, and that it could be possible for someone to think that the forbidden_if rule will cause a 403 if the conditions are not met.

@driesvints
Copy link
Member

fail_if & fail_unless maybe?

@lk77
Copy link

lk77 commented Mar 9, 2021

In that case i think that you should have an accessor, not an actual field on the database

public function getIsDeceasedAttribute()
{
     return $this->date_of_death !== null
}

it avoid storing the same information twice, and having possible inconsistencies

@jessarcher
Copy link
Member Author

jessarcher commented Mar 9, 2021

In that case i think that you should have an accessor, not an actual field on the database

I would usually agree, but in this case there is a clinical requirement to be able to store both, with the date being optional as it may not be known.

@taylorotwell taylorotwell merged commit a4678ce into laravel:8.x Mar 9, 2021
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.

6 participants