diff --git a/src/Illuminate/Validation/Concerns/ReplacesAttributes.php b/src/Illuminate/Validation/Concerns/ReplacesAttributes.php index 8062ab9579f3..87d7f24b0962 100644 --- a/src/Illuminate/Validation/Concerns/ReplacesAttributes.php +++ b/src/Illuminate/Validation/Concerns/ReplacesAttributes.php @@ -414,6 +414,24 @@ protected function replaceProhibitedUnless($message, $attribute, $rule, $paramet return str_replace([':other', ':values'], [$other, implode(', ', $values)], $message); } + /** + * Replace all place-holders for the accepted_if rule. + * + * @param string $message + * @param string $attribute + * @param string $rule + * @param array $parameters + * @return string + */ + protected function replaceAcceptedIf($message, $attribute, $rule, $parameters) + { + $parameters[1] = $this->getDisplayableValue($parameters[0], Arr::get($this->data, $parameters[0])); + + $parameters[0] = $this->getDisplayableAttribute($parameters[0]); + + return str_replace([':other', ':value'], $parameters, $message); + } + /** * Replace all place-holders for the same rule. * diff --git a/tests/Validation/ValidationValidatorTest.php b/tests/Validation/ValidationValidatorTest.php index 57668851bc0f..d6312081068c 100755 --- a/tests/Validation/ValidationValidatorTest.php +++ b/tests/Validation/ValidationValidatorTest.php @@ -1806,6 +1806,37 @@ public function testValidateAcceptedIf() $v = new Validator($trans, ['foo' => 'true', 'bar' => 'aaa'], ['foo' => 'accepted_if:bar,aaa']); $this->assertTrue($v->passes()); + + // accepted_if:bar,aaa + $trans = $this->getIlluminateArrayTranslator(); + $trans->addLines(['validation.accepted_if' => 'The :attribute field must be accepted when :other is :value.'], 'en'); + $v = new Validator($trans, ['foo' => 'no', 'bar' => 'aaa'], ['foo' => 'accepted_if:bar,aaa']); + $this->assertFalse($v->passes()); + $v->messages()->setFormat(':message'); + $this->assertSame('The foo field must be accepted when bar is aaa.', $v->messages()->first('foo')); + + // accepted_if:bar,aaa,... + $trans = $this->getIlluminateArrayTranslator(); + $trans->addLines(['validation.accepted_if' => 'The :attribute field must be accepted when :other is :value.'], 'en'); + $v = new Validator($trans, ['foo' => 'no', 'bar' => 'abc'], ['foo' => 'accepted_if:bar,aaa,bbb,abc']); + $this->assertFalse($v->passes()); + $v->messages()->setFormat(':message'); + $this->assertSame('The foo field must be accepted when bar is abc.', $v->messages()->first('foo')); + + // accepted_if:bar,boolean + $trans = $this->getIlluminateArrayTranslator(); + $trans->addLines(['validation.accepted_if' => 'The :attribute field must be accepted when :other is :value.'], 'en'); + $v = new Validator($trans, ['foo' => 'no', 'bar' => false], ['foo' => 'accepted_if:bar,false']); + $this->assertFalse($v->passes()); + $v->messages()->setFormat(':message'); + $this->assertSame('The foo field must be accepted when bar is false.', $v->messages()->first('foo')); + + $trans = $this->getIlluminateArrayTranslator(); + $trans->addLines(['validation.accepted_if' => 'The :attribute field must be accepted when :other is :value.'], 'en'); + $v = new Validator($trans, ['foo' => 'no', 'bar' => true], ['foo' => 'accepted_if:bar,true']); + $this->assertFalse($v->passes()); + $v->messages()->setFormat(':message'); + $this->assertSame('The foo field must be accepted when bar is true.', $v->messages()->first('foo')); } public function testValidateEndsWith()