This repository was archived by the owner on Jan 30, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 49
Fix #28 - NotEmpty validation message cannot be localized #67
Merged
weierophinney
merged 3 commits into
zendframework:master
from
stefanotorresi:fix-input-not-empty-message-i18n
Apr 7, 2016
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This works in tests. But in an app, translators are directly injected via the plugin manager. |
||
} | ||
|
||
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; | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Translator will always be empty here because you're directly instantiating it. Try pulling it from the validator chain instead, add that will likely use the plugin manager, and thus have access to the translator.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm I beg to differ. This actually works in a real application.
The test reproduces the issue with the configuration suggested in http://framework.zend.com/manual/current/en/modules/zend.validator.messages.html#using-pre-translated-validation-messages
I also have a functional test in a real world application that reproduces it exactly in the same way.
The translator will not be empty even in new instances, as long as
Zend\Validator\AbstractValidator::setDefaultTranslator()
is used;I tried avoding the static method usage, but in my experience the ValidatorPluginManager does not work consistently and I ended up with the translator not being correctly injected.
I can change the
new
statement with aplugin()
invocation from the validator chain, but the static method call will still be necessary.