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

[9.x] Add RequiredIfAccepted validation rule #44035

Merged
merged 3 commits into from
Sep 7, 2022
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
16 changes: 16 additions & 0 deletions src/Illuminate/Validation/Concerns/ReplacesAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,22 @@ protected function replaceRequiredIf($message, $attribute, $rule, $parameters)
return str_replace([':other', ':value'], $parameters, $message);
}

/**
* Replace all place-holders for the required_if_accepted rule.
*
* @param string $message
* @param string $attribute
* @param string $rule
* @param array<int,string> $parameters
* @return string
*/
protected function replaceRequiredIfAccepted($message, $attribute, $rule, $parameters)
{
$parameters[0] = $this->getDisplayableAttribute($parameters[0]);

return str_replace([':other'], $parameters, $message);
}

/**
* Replace all place-holders for the required_unless rule.
*
Expand Down
19 changes: 19 additions & 0 deletions src/Illuminate/Validation/Concerns/ValidatesAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -1601,6 +1601,25 @@ public function validateRequiredIf($attribute, $value, $parameters)
return true;
}

/**
* Validate that an attribute exists when another attribute was "accepted".
*
* @param string $attribute
* @param mixed $value
* @param mixed $parameters
* @return bool
*/
public function validateRequiredIfAccepted($attribute, $value, $parameters)
{
$this->requireParameterCount(1, $parameters, 'required_if_accepted');

if ($this->validateAccepted($parameters[0], $this->getValue($parameters[0]))) {
return $this->validateRequired($attribute, $value);
}

return true;
}

/**
* Validate that an attribute does not exist.
*
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 @@ -210,6 +210,7 @@ class Validator implements ValidatorContract
'Present',
'Required',
'RequiredIf',
'RequiredIfAccepted',
'RequiredUnless',
'RequiredWith',
'RequiredWithAll',
Expand Down Expand Up @@ -240,6 +241,7 @@ class Validator implements ValidatorContract
'AcceptedIf',
'DeclinedIf',
'RequiredIf',
'RequiredIfAccepted',
'RequiredUnless',
'RequiredWith',
'RequiredWithAll',
Expand Down
16 changes: 16 additions & 0 deletions tests/Validation/ValidationValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1972,6 +1972,22 @@ public function testValidateAccepted()
$this->assertTrue($v->passes());
}

public function testValidateRequiredAcceptedIf()
{
$trans = $this->getIlluminateArrayTranslator();
$v = new Validator($trans, ['foo' => 'no', 'bar' => 'baz'], ['bar' => 'required_if_accepted:foo']);
$this->assertTrue($v->passes());

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

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

$v = new Validator($trans, ['foo' => 'yes', 'bar' => ''], ['bar' => 'required_if_accepted:foo']);
$this->assertFalse($v->passes());
}

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