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

Added accepted_if validation rule #38210

Merged
merged 3 commits into from
Aug 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions src/Illuminate/Validation/Concerns/ValidatesAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,30 @@ public function validateAccepted($attribute, $value)
return $this->validateRequired($attribute, $value) && in_array($value, $acceptable, true);
}

/**
* Validate that an attribute was "accepted" when another attribute has a given value.
*
* @param string $attribute
* @param mixed $value
* @param mixed $parameters
* @return bool
*/
public function validateAcceptedIf($attribute, $value, $parameters)
{

$acceptable = ['yes', 'on', '1', 1, true, 'true'];

$this->requireParameterCount(2, $parameters, 'accepted_if');

[$values, $other] = $this->parseDependentRuleParameters($parameters);

if (in_array($other, $values, is_bool($other) || is_null($other))) {
return $this->validateRequired($attribute, $value) && in_array($value, $acceptable, true);
}

return true;
}

/**
* Validate that an attribute is an active URL.
*
Expand Down
2 changes: 2 additions & 0 deletions src/Illuminate/Validation/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ class Validator implements ValidatorContract
*/
protected $implicitRules = [
'Accepted',
'AcceptedIf',
'Filled',
'Present',
'Required',
Expand Down Expand Up @@ -231,6 +232,7 @@ class Validator implements ValidatorContract
'Gte',
'Lt',
'Lte',
'AcceptedIf',
'RequiredIf',
'RequiredUnless',
'RequiredWith',
Expand Down
38 changes: 38 additions & 0 deletions tests/Validation/ValidationValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1771,6 +1771,44 @@ public function testValidateAccepted()
$this->assertTrue($v->passes());
}

public function testValidateAcceptedIf()
{

$trans = $this->getIlluminateArrayTranslator();
$v = new Validator($trans, ['foo' => 'no', 'bar' => 'aaa'], ['foo' => 'accepted_if:bar,aaa']);
$this->assertFalse($v->passes());

$v = new Validator($trans, ['foo' => null, 'bar' => 'aaa'], ['foo' => 'accepted_if:bar,aaa']);
$this->assertFalse($v->passes());

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

$v = new Validator($trans, ['foo' => false, 'bar' => 'aaa'], ['foo' => 'accepted_if:bar,aaa']);
$this->assertFalse($v->passes());

$v = new Validator($trans, ['foo' => 'false', 'bar' => 'aaa'], ['foo' => 'accepted_if:bar,aaa']);
$this->assertFalse($v->passes());

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

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

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

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

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

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

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