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

Commit

Permalink
Implements setTranslateMessages() for en/dis-abling translations
Browse files Browse the repository at this point in the history
This patch modifies `FormElementErrors` to add a new method,
`setTranslateMessages(bool $flag)`. By default, and per #104,
translations are enabled. However, by calling the method with `false`,
you can disable translations.
  • Loading branch information
weierophinney committed May 16, 2018
1 parent f49af08 commit 455ab01
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 21 deletions.
88 changes: 71 additions & 17 deletions src/View/Helper/FormElementErrors.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ class FormElementErrors extends AbstractHelper
*/
protected $attributes = [];

/**
* @var bool Whether or not to translate error messages during render.
*/
protected $translateErrorMessages = true;

/**
* Invoke helper as functor
*
Expand All @@ -49,6 +54,10 @@ public function __invoke(ElementInterface $element = null, array $attributes = [
/**
* Render validation errors for the provided $element
*
* If {@link $translateErrorMessages} is true, and a translator is
* composed, messages retrieved from the element will be translated; if
* either is not the case, they will not.
*
* @param ElementInterface $element
* @param array $attributes
* @throws Exception\DomainException
Expand All @@ -60,39 +69,32 @@ public function render(ElementInterface $element, array $attributes = [])
if (empty($messages)) {
return '';
}
if (! is_array($messages) && ! $messages instanceof Traversable) {

$messages = $messages instanceof Traversable ? iterator_to_array($messages) : $messages;
if (! is_array($messages)) {
throw new Exception\DomainException(sprintf(
'%s expects that $element->getMessages() will return an array or Traversable; received "%s"',
__METHOD__,
(is_object($messages) ? get_class($messages) : gettype($messages))
));
}

// Flatten message array
$messages = $this->flattenMessages($messages);
if (empty($messages)) {
return '';
}

// Prepare attributes for opening tag
$attributes = array_merge($this->attributes, $attributes);
$attributes = $this->createAttributesString($attributes);
if (! empty($attributes)) {
$attributes = ' ' . $attributes;
}

// Flatten message array
$escapeHtml = $this->getEscapeHtmlHelper();
$messagesToPrint = [];
$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 '';
}

// Generate markup
$markup = sprintf($this->getMessageOpenFormat(), $attributes);
$markup .= implode($this->getMessageSeparatorString(), $messagesToPrint);
$markup .= implode($this->getMessageSeparatorString(), $messages);
$markup .= $this->getMessageCloseString();

return $markup;
Expand Down Expand Up @@ -185,4 +187,56 @@ public function getMessageSeparatorString()
{
return $this->messageSeparatorString;
}

/**
* Set the flag detailing whether or not to translate error messages.
*
* @param bool $flag
* @return self
*/
public function setTranslateMessages($flag)
{
$this->translateErrorMessages = (bool) $flag;
return $this;
}

/**
* @param array $messages
* @return array
*/
private function flattenMessages(array $messages)
{
return $this->translateErrorMessages && $this->getTranslator()
? $this->flattenMessagesWithTranslator($messages)
: $this->flattenMessagesWithoutTranslator($messages);
}

/**
* @param array $messages
* @return array
*/
private function flattenMessagesWithoutTranslator(array $messages)
{
$messagesToPrint = [];
array_walk_recursive($messages, function ($item) use (&$messagesToPrint) {
$messagesToPrint[] = $item;
});
return $messagesToPrint;
}

/**
* @param array $messages
* @return array
*/
private function flattenMessagesWithTranslator(array $messages)
{
$translator = $this->getTranslator();
$textDomain = $this->getTranslatorTextDomain();
$messagesToPrint = [];
$messageCallback = function ($item) use (&$messagesToPrint, $translator, $textDomain) {
$messagesToPrint[] = $translator->translate($item, $textDomain);
};
array_walk_recursive($messages, $messageCallback);
return $messagesToPrint;
}
}
8 changes: 4 additions & 4 deletions test/View/Helper/FormElementErrorsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,12 +128,12 @@ function ($message) {

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

// Disable translation...
$this->helper->setTranslateMessages(false);

$markup = $this->helper->render($form->get('test_element'));

$this->assertRegexp(
'#<ul>\s*<li>TRANSLATED: The input does not match against pattern \'/^#[0-9a-fA-F]{6}$/\'</li>\s*</ul>#s',
$markup
);
$this->assertRegexp('#^<ul>\s*<li>TRANSLATED#s', $markup);
}

public function testCanSpecifyAttributesForOpeningTag()
Expand Down

0 comments on commit 455ab01

Please sign in to comment.