Skip to content

Commit

Permalink
[9.x] Add RequiredIfAccepted validation rule (#44035)
Browse files Browse the repository at this point in the history
* RequiredIfAccepted validation rule

* RequiredIfAccepted attribute placeholder replacer + styling fixes

* Update ValidatesAttributes.php
  • Loading branch information
pascalbaljet authored Sep 7, 2022
1 parent fc9dc59 commit 88f8b02
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 0 deletions.
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

0 comments on commit 88f8b02

Please sign in to comment.