Skip to content

Commit

Permalink
[11.x] create new "has" validation rule (#51348)
Browse files Browse the repository at this point in the history
* create new "has" validation rule

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.

* minor styling

* formatting

---------

Co-authored-by: Taylor Otwell <[email protected]>
  • Loading branch information
browner12 and taylorotwell authored May 9, 2024
1 parent 892da9e commit 4815757
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
23 changes: 23 additions & 0 deletions src/Illuminate/Validation/Concerns/ValidatesAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -500,6 +500,29 @@ public function validateConfirmed($attribute, $value)
return $this->validateSame($attribute, $value, [$attribute.'_confirmation']);
}

/**
* Validate an attribute has a list of values.
*
* @param string $attribute
* @param mixed $value
* @param array<int, int|string> $parameters
* @return bool
*/
public function validateContains($attribute, $value, $parameters)
{
if (! is_array($value)) {
return false;
}

foreach ($parameters as $parameter) {
if (! in_array($parameter, $value)) {
return false;
}
}

return true;
}

/**
* Validate that the password of the currently authenticated user matches the given value.
*
Expand Down
23 changes: 23 additions & 0 deletions tests/Validation/ValidationValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3789,6 +3789,29 @@ public function testValidateLtePlaceHolderIsReplacedProperly()
$this->assertEquals(2, $v->messages()->first('items'));
}

public function testValidateContains()
{
$trans = $this->getIlluminateArrayTranslator();

$v = new Validator($trans, ['name' => 0], ['name' => 'contains:bar']);
$this->assertFalse($v->passes());

$v = new Validator($trans, ['name' => ['foo', 'bar']], ['name' => 'contains:baz']);
$this->assertFalse($v->passes());

$v = new Validator($trans, ['name' => ['foo', 'bar']], ['name' => 'contains:baz,buzz']);
$this->assertFalse($v->passes());

$v = new Validator($trans, ['name' => ['foo', 'bar']], ['name' => 'contains:foo,buzz']);
$this->assertFalse($v->passes());

$v = new Validator($trans, ['name' => ['foo', 'bar']], ['name' => 'contains:foo']);
$this->assertTrue($v->passes());

$v = new Validator($trans, ['name' => ['foo', 'bar']], ['name' => 'contains:foo,bar']);
$this->assertTrue($v->passes());
}

public function testValidateIn()
{
$trans = $this->getIlluminateArrayTranslator();
Expand Down

0 comments on commit 4815757

Please sign in to comment.