diff --git a/src/Illuminate/Validation/Concerns/ReplacesAttributes.php b/src/Illuminate/Validation/Concerns/ReplacesAttributes.php index c1039814eaca..5df631ac494a 100644 --- a/src/Illuminate/Validation/Concerns/ReplacesAttributes.php +++ b/src/Illuminate/Validation/Concerns/ReplacesAttributes.php @@ -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 $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. * diff --git a/src/Illuminate/Validation/Concerns/ValidatesAttributes.php b/src/Illuminate/Validation/Concerns/ValidatesAttributes.php index fe4644257f3f..c75c8d7f4638 100644 --- a/src/Illuminate/Validation/Concerns/ValidatesAttributes.php +++ b/src/Illuminate/Validation/Concerns/ValidatesAttributes.php @@ -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. * diff --git a/src/Illuminate/Validation/Validator.php b/src/Illuminate/Validation/Validator.php index 74e360bccd54..cc104ac5ac7b 100755 --- a/src/Illuminate/Validation/Validator.php +++ b/src/Illuminate/Validation/Validator.php @@ -210,6 +210,7 @@ class Validator implements ValidatorContract 'Present', 'Required', 'RequiredIf', + 'RequiredIfAccepted', 'RequiredUnless', 'RequiredWith', 'RequiredWithAll', @@ -240,6 +241,7 @@ class Validator implements ValidatorContract 'AcceptedIf', 'DeclinedIf', 'RequiredIf', + 'RequiredIfAccepted', 'RequiredUnless', 'RequiredWith', 'RequiredWithAll', diff --git a/tests/Validation/ValidationValidatorTest.php b/tests/Validation/ValidationValidatorTest.php index 5d9d4cf9ed34..7f3a903b6b7f 100755 --- a/tests/Validation/ValidationValidatorTest.php +++ b/tests/Validation/ValidationValidatorTest.php @@ -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();