From af8837d6f76cddff7939601def2095357ab5e77b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20Nystr=C3=B6m?= Date: Wed, 7 Oct 2015 16:04:07 +0200 Subject: [PATCH 1/2] Test that attached NotEmpty validator message is used --- test/InputTest.php | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) 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 From 4fc84cd93fd90da68f216927ef6226c97927e330 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20Nystr=C3=B6m?= Date: Wed, 7 Oct 2015 16:05:36 +0200 Subject: [PATCH 2/2] Fix #69 - Use attached NotEmpty message when required validation fails --- src/Input.php | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) 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],