Skip to content

Commit

Permalink
create new "has" validation rule
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
browner12 committed May 8, 2024
1 parent 2057d55 commit 8ed0418
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 @@ -1303,6 +1303,29 @@ public function validateUppercase($attribute, $value, $parameters)
return Str::upper($value) === $value;
}

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

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

return true;
}

/**
* Validate that an attribute is a valid HEX color.
*
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 testValidateHas()
{
$trans = $this->getIlluminateArrayTranslator();

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

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

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

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

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

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

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

0 comments on commit 8ed0418

Please sign in to comment.