Skip to content

Commit

Permalink
[11.x] Add RequiredIfDeclined validation rule (#51030)
Browse files Browse the repository at this point in the history
  • Loading branch information
timmydhooghe authored Apr 12, 2024
1 parent dec31b0 commit 393ccc6
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 @@ -609,6 +609,22 @@ protected function replaceRequiredIfAccepted($message, $attribute, $rule, $param
return str_replace([':other'], $parameters, $message);
}

/**
* Replace all place-holders for the required_if_declined rule.
*
* @param string $message
* @param string $attribute
* @param string $rule
* @param array<int,string> $parameters
* @return string
*/
public function replaceRequiredIfDeclined($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 @@ -1961,6 +1961,25 @@ public function validateRequiredIfAccepted($attribute, $value, $parameters)
return true;
}

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

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

return true;
}

/**
* Validate that an attribute does not exist or is an empty string.
*
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 @@ -221,6 +221,7 @@ class Validator implements ValidatorContract
'Required',
'RequiredIf',
'RequiredIfAccepted',
'RequiredIfDeclined',
'RequiredUnless',
'RequiredWith',
'RequiredWithAll',
Expand Down Expand Up @@ -252,6 +253,7 @@ class Validator implements ValidatorContract
'DeclinedIf',
'RequiredIf',
'RequiredIfAccepted',
'RequiredIfDeclined',
'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 @@ -2447,6 +2447,22 @@ public function testValidateAcceptedIf()
$this->assertSame('The foo field must be accepted when bar is true.', $v->messages()->first('foo'));
}

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

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

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

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

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

0 comments on commit 393ccc6

Please sign in to comment.