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

Commit

Permalink
Merge branch 'feature/104' into develop
Browse files Browse the repository at this point in the history
Close #104
  • Loading branch information
weierophinney committed Dec 6, 2017
2 parents 1dbf69b + bce9708 commit d29b881
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 3 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ All notable changes to this project will be documented in this file, in reverse

### Added

- Nothing.
- [#104](https://github.com/zendframework/zend-form/pull/104) adds the ability
for the `FormElementErrors` view helper to translate validation error messages
using the composed translator and text domain instances.

### Deprecated

Expand Down
8 changes: 6 additions & 2 deletions src/View/Helper/FormElementErrors.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,13 @@ public function render(ElementInterface $element, array $attributes = [])
// Flatten message array
$escapeHtml = $this->getEscapeHtmlHelper();
$messagesToPrint = [];
array_walk_recursive($messages, function ($item) use (&$messagesToPrint, $escapeHtml) {
$translator = $this->getTranslator();
$textDomain = $this->getTranslatorTextDomain();
$messageCallback = function ($item) use (&$messagesToPrint, $escapeHtml, $translator, $textDomain) {
$item = $translator ? $translator->translate($item, $textDomain) : $item;
$messagesToPrint[] = $escapeHtml($item);
});
};
array_walk_recursive($messages, $messageCallback);

if (empty($messagesToPrint)) {
return '';
Expand Down
26 changes: 26 additions & 0 deletions test/View/Helper/FormElementErrorsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,32 @@ public function testRendersErrorMessagesUsingUnorderedListByDefault()
// @codingStandardsIgnoreEnd
}

public function testRendersErrorMessagesUsingUnorderedListTranslated()
{
$mockTranslator = $this->createMock('Zend\I18n\Translator\Translator');
$mockTranslator->expects($this->at(0))
->method('translate')
->will($this->returnValue('Translated first error message'));
$mockTranslator->expects($this->at(1))
->method('translate')
->will($this->returnValue('Translated second error message'));
$mockTranslator->expects($this->at(2))
->method('translate')
->will($this->returnValue('Translated third error message'));

$this->helper->setTranslator($mockTranslator);
$this->assertTrue($this->helper->hasTranslator());

$this->helper->setTranslatorTextDomain('default');

$messages = $this->getMessageList();
$element = new Element('foo');
$element->setMessages($messages);

$markup = $this->helper->render($element);
$this->assertRegexp('#<ul>\s*<li>Translated first error message</li>\s*<li>Translated second error message</li>\s*<li>Translated third error message</li>\s*</ul>#s', $markup);
}

public function testCanSpecifyAttributesForOpeningTag()
{
$messages = $this->getMessageList();
Expand Down

0 comments on commit d29b881

Please sign in to comment.