Skip to content
This repository was archived by the owner on Jan 30, 2020. It is now read-only.

Commit aab0982

Browse files
committed
Merge pull request #67 from stefanotorresi/fix-input-not-empty-message-i18n
Fix #28 - NotEmpty validation message cannot be localized
2 parents 91d5795 + cf1f691 commit aab0982

File tree

2 files changed

+37
-3
lines changed

2 files changed

+37
-3
lines changed

src/Input.php

+10-3
Original file line numberDiff line numberDiff line change
@@ -491,10 +491,17 @@ protected function injectNotEmptyValidator()
491491
*/
492492
protected function prepareRequiredValidationFailureMessage()
493493
{
494-
$notEmpty = new NotEmpty();
495-
$templates = $notEmpty->getOption('messageTemplates');
494+
$notEmpty = $this->getValidatorChain()->plugin(NotEmpty::class);
495+
$templates = $notEmpty->getOption('messageTemplates');
496+
$message = $templates[NotEmpty::IS_EMPTY];
497+
$translator = $notEmpty->getTranslator();
498+
499+
if ($translator) {
500+
$message = $translator->translate($message, $notEmpty->getTranslatorTextDomain());
501+
}
502+
496503
return [
497-
NotEmpty::IS_EMPTY => $templates[NotEmpty::IS_EMPTY],
504+
NotEmpty::IS_EMPTY => $message,
498505
];
499506
}
500507
}

test/InputTest.php

+27
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@
1515
use Zend\Filter\FilterChain;
1616
use Zend\InputFilter\Input;
1717
use Zend\InputFilter\InputInterface;
18+
use Zend\Validator\AbstractValidator;
1819
use Zend\Validator\NotEmpty as NotEmptyValidator;
20+
use Zend\Validator\Translator\TranslatorInterface;
1921
use Zend\Validator\ValidatorChain;
2022
use Zend\Validator\ValidatorInterface;
2123

@@ -34,6 +36,11 @@ public function setUp()
3436
$this->input = new Input('foo');
3537
}
3638

39+
protected function tearDown()
40+
{
41+
AbstractValidator::setDefaultTranslator(null);
42+
}
43+
3744
public function assertRequiredValidationErrorMessage(Input $input, $message = '')
3845
{
3946
$message = $message ?: 'Expected failure message for required input';
@@ -569,6 +576,26 @@ public function testInputMergeWithTargetValue()
569576
$this->assertTrue($target->hasValue(), 'hasValue() value not match');
570577
}
571578

579+
public function testNotEmptyMessageIsTranslated()
580+
{
581+
/** @var TranslatorInterface|MockObject $translator */
582+
$translator = $this->getMock(TranslatorInterface::class);
583+
AbstractValidator::setDefaultTranslator($translator);
584+
$notEmpty = new NotEmptyValidator();
585+
586+
$translatedMessage = 'some translation';
587+
$translator->expects($this->atLeastOnce())
588+
->method('translate')
589+
->with($notEmpty->getMessageTemplates()[NotEmptyValidator::IS_EMPTY])
590+
->willReturn($translatedMessage)
591+
;
592+
593+
$this->assertFalse($this->input->isValid());
594+
$messages = $this->input->getMessages();
595+
$this->assertArrayHasKey('isEmpty', $messages);
596+
$this->assertSame($translatedMessage, $messages['isEmpty']);
597+
}
598+
572599
public function fallbackValueVsIsValidProvider()
573600
{
574601
$required = true;

0 commit comments

Comments
 (0)