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 validation rule #36667

Merged
merged 1 commit into from
Mar 19, 2021
Merged

[8.x] Add prohibited validation rule #36667

merged 1 commit into from
Mar 19, 2021

Conversation

PhiloNL
Copy link
Contributor

@PhiloNL PhiloNL commented Mar 19, 2021

In addition to the recently added prohibited_if and prohibited_unless validation rules (#36516), I think a generic prohibited validation rule may be useful.

Use case
In my specific use case, the prohibited validation rule is useful when building API's while working with immutable attributes, for example:

// Create a license key
$this->postJson(route('api.license'),['name' => 'foobar', 'key' => 'my-key']));

// Update given license key
$this->patchJson(route('api.license', '123-456'),['name' => 'hello-world', 'key' => 'random-key']));

The license key is immutable and for that reason it's not part of the validation rules, so the API responds with a successful status given it just ignores the key attribute:

// PUT /api/licenses/123-456
// {"name":"hello-world", "key":"random-key"}

$validated = $request->validate([
    'name' => 'required|max:255',
]);

$license->update($validated);

// Response: OK

When working with API's this might be confusing. This might give the impression that the request was successful and both the name and key have been updated while in reality only the name has been changed. The prohibited validation rule will throw a validation error if a prohibited attribute is submitted.

// PUT /api/licenses/123-456
// {"name":"hello-world", "key":"random-key"}

$validated = $request->validate([
    'name' => 'required|max:255',
    'key' => 'prohibited',
]);

// Response: 422
// The key field is prohibited

@PhiloNL PhiloNL changed the title Add prohibited validation rule [8.x] Add prohibited validation rule Mar 19, 2021
@taylorotwell taylorotwell merged commit 0de4d79 into laravel:8.x Mar 19, 2021
@PhiloNL PhiloNL deleted the feature/prohibit branch March 19, 2021 14:25
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.

None yet

2 participants