diff --git a/src/Input.php b/src/Input.php index 6342040b..9d316c0b 100644 --- a/src/Input.php +++ b/src/Input.php @@ -491,7 +491,20 @@ protected function injectNotEmptyValidator() */ protected function prepareRequiredValidationFailureMessage() { - $notEmpty = new NotEmpty(); + $chain = $this->getValidatorChain(); + $validators = $chain->getValidators(); + + foreach ($validators as $validator) { + if ($validator['instance'] instanceof NotEmpty) { + $notEmpty = $validator['instance']; + break; + } + } + + if (!isset($notEmpty)) { + $notEmpty = new NotEmpty(); + } + $templates = $notEmpty->getOption('messageTemplates'); return [ NotEmpty::IS_EMPTY => $templates[NotEmpty::IS_EMPTY], diff --git a/test/InputTest.php b/test/InputTest.php index e7dd27ae..26db9595 100644 --- a/test/InputTest.php +++ b/test/InputTest.php @@ -220,6 +220,39 @@ public function testRequiredWithoutFallbackAndValueNotSetProvidesNotEmptyValidat $this->assertRequiredValidationErrorMessage($input); } + /** + * @group 28 + * @group 69 + */ + public function testRequiredWithoutFallbackAndValueNotSetProvidesAttachedNotEmptyValidatorIsEmptyErrorMessage() + { + $input = new Input(); + $input->setRequired(true); + + $customMessage = [ + NotEmptyValidator::IS_EMPTY => "Custom message", + ]; + + $notEmpty = $this->getMockBuilder(NotEmptyValidator::class) + ->setMethods(['getOption']) + ->getMock(); + + $notEmpty->expects($this->once()) + ->method('getOption') + ->with('messageTemplates') + ->willReturn($customMessage); + + $input->getValidatorChain() + ->attach($notEmpty); + + $this->assertFalse( + $input->isValid(), + 'isValid() should always return false when no fallback value is present, ' + . 'the input is required, and no data is set.' + ); + $this->assertEquals($customMessage, $input->getMessages()); + } + /** * @group 28 * @group 60