diff --git a/src/Input.php b/src/Input.php index 6342040b..456ba8a4 100644 --- a/src/Input.php +++ b/src/Input.php @@ -491,10 +491,17 @@ protected function injectNotEmptyValidator() */ protected function prepareRequiredValidationFailureMessage() { - $notEmpty = new NotEmpty(); - $templates = $notEmpty->getOption('messageTemplates'); + $notEmpty = $this->getValidatorChain()->plugin(NotEmpty::class); + $templates = $notEmpty->getOption('messageTemplates'); + $message = $templates[NotEmpty::IS_EMPTY]; + $translator = $notEmpty->getTranslator(); + + if ($translator) { + $message = $translator->translate($message, $notEmpty->getTranslatorTextDomain()); + } + return [ - NotEmpty::IS_EMPTY => $templates[NotEmpty::IS_EMPTY], + NotEmpty::IS_EMPTY => $message, ]; } } diff --git a/test/InputTest.php b/test/InputTest.php index e7dd27ae..94cbf55c 100644 --- a/test/InputTest.php +++ b/test/InputTest.php @@ -15,7 +15,9 @@ use Zend\Filter\FilterChain; use Zend\InputFilter\Input; use Zend\InputFilter\InputInterface; +use Zend\Validator\AbstractValidator; use Zend\Validator\NotEmpty as NotEmptyValidator; +use Zend\Validator\Translator\TranslatorInterface; use Zend\Validator\ValidatorChain; use Zend\Validator\ValidatorInterface; @@ -34,6 +36,11 @@ public function setUp() $this->input = new Input('foo'); } + protected function tearDown() + { + AbstractValidator::setDefaultTranslator(null); + } + public function assertRequiredValidationErrorMessage(Input $input, $message = '') { $message = $message ?: 'Expected failure message for required input'; @@ -569,6 +576,26 @@ public function testInputMergeWithTargetValue() $this->assertTrue($target->hasValue(), 'hasValue() value not match'); } + public function testNotEmptyMessageIsTranslated() + { + /** @var TranslatorInterface|MockObject $translator */ + $translator = $this->getMock(TranslatorInterface::class); + AbstractValidator::setDefaultTranslator($translator); + $notEmpty = new NotEmptyValidator(); + + $translatedMessage = 'some translation'; + $translator->expects($this->atLeastOnce()) + ->method('translate') + ->with($notEmpty->getMessageTemplates()[NotEmptyValidator::IS_EMPTY]) + ->willReturn($translatedMessage) + ; + + $this->assertFalse($this->input->isValid()); + $messages = $this->input->getMessages(); + $this->assertArrayHasKey('isEmpty', $messages); + $this->assertSame($translatedMessage, $messages['isEmpty']); + } + public function fallbackValueVsIsValidProvider() { $required = true;