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

Commit

Permalink
Merge branch 'hotfix/67' into develop
Browse files Browse the repository at this point in the history
Forward port #67
  • Loading branch information
weierophinney committed Apr 7, 2016
2 parents 5da0a82 + e3888ba commit bdc3929
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 4 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ All notable changes to this project will be documented in this file, in reverse

### Fixed

- Nothing.
- [#67](https://github.com/zendframework/zend-inputfilter/pull/67) fixes
localization of the `NotEmpty` validation error message (created for any
required input for which a value was not provided).

## 2.6.0 - 2016-02-18

Expand Down
13 changes: 10 additions & 3 deletions src/Input.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
];
}
}
27 changes: 27 additions & 0 deletions test/InputTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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';
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit bdc3929

Please sign in to comment.