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] create new "has" validation rule #51348

Merged
merged 3 commits into from
May 9, 2024

Conversation

browner12
Copy link
Contributor

currently we have the "in" rule, which checks if given input is in a list of approved values. this new "has" rule is kind of the opposite of that. it checks that expected values are included in the given array of input.

for example, assume you are setting up the ability to restrict access to certain IP addresses. you might setup some rules like this:

return [
    'allowed_ips'   => ['present', 'nullable', 'array'],
    'allowed_ips.*' => ['required', 'ip'],
];

However, you want to make sure the current user's current IP address is included in the provided array of input. Current rules do not provide a way to do this. With the new has rule, the rules would change to:

return [
    'allowed_ips'   => ['present', 'nullable', 'array', 'has:' . $request->ip()],
    'allowed_ips.*' => ['required', 'ip'],
];

You may also pass multiple parameters to the has rule, which would require all of the passed parameters to exist in the given input.

currently we have the "in" rule, which checks if given input is in a list of approved values. this new "has" rule is kind of the opposite of that. it checks that expected values are included in the given array of input.

for example, assume you are setting up the ability to restrict access to certain IP addresses. you might setup some rules like this:

```php
return [
    'allowed_ips'   => ['present', 'nullable', 'array'],
    'allowed_ips.*' => ['required', 'ip'],
];
```

However, you want to make sure the current user's current IP address is included in the provided array of input. Current rules do not provide a way to do this. With the new `has` rule, the rules would change to:

```php
return [
    'allowed_ips'   => ['present', 'nullable', 'array', 'has:' . $request->ip()],
    'allowed_ips.*' => ['required', 'ip'],
];
```

You may also pass multiple parameters to the `has` rule, which would require all of the passed parameters to exist in the given input.
@Jacobs63
Copy link
Contributor

Jacobs63 commented May 9, 2024

Based on the naming, I'd expect this to work as \Illuminate\Support\Arr::has, however, that is not the case.

Perhaps naming it to contains would make more sense?

I like the idea 👍

@browner12
Copy link
Contributor Author

I like "contains" as well. Open to whatever the maintainers decide on.

@taylorotwell taylorotwell merged commit 4815757 into laravel:11.x May 9, 2024
27 of 28 checks passed
@browner12 browner12 deleted the AB-has-validation-rule branch May 9, 2024 19:36
browner12 added a commit to browner12/docs that referenced this pull request May 9, 2024
taylorotwell added a commit to laravel/docs that referenced this pull request May 10, 2024
* add documentation for new "contains" validation rule

laravel/framework#51348

* Update validation.md

---------

Co-authored-by: Taylor Otwell <taylor@laravel.com>
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.

3 participants