Add Icinga\Web\Form\ErrorLabeller

refs #8415
This commit is contained in:
Johannes Meyer 2015-02-12 09:06:34 +01:00
parent aa473fb8cf
commit 9e933d835d
2 changed files with 67 additions and 0 deletions

View File

@ -12,6 +12,7 @@ use Icinga\Application\Icinga;
use Icinga\Authentication\Manager;
use Icinga\Security\SecurityException;
use Icinga\Util\Translator;
use Icinga\Web\Form\ErrorLabeller;
use Icinga\Web\Form\Decorator\NoScriptApply;
use Icinga\Web\Form\Element\CsrfCounterMeasure;
@ -520,6 +521,7 @@ class Form extends Zend_Form
}
$el = parent::createElement($type, $name, $options);
$el->setTranslator(new ErrorLabeller(array('element' => $el)));
$el->addPrefixPaths(array(
array(

View File

@ -0,0 +1,65 @@
<?php
/* Icinga Web 2 | (c) 2013-2015 Icinga Development Team | GPLv2+ */
namespace Icinga\Web\Form;
use BadMethodCallException;
use Zend_Translate_Adapter;
use Zend_Validate_NotEmpty;
use Icinga\Web\Form\Validator\DateTimeValidator;
use Icinga\Web\Form\Validator\ReadablePathValidator;
use Icinga\Web\Form\Validator\WritablePathValidator;
class ErrorLabeller extends Zend_Translate_Adapter
{
protected $messages;
public function __construct($options = array())
{
if (! isset($options['element'])) {
throw new BadMethodCallException('Option "element" is missing');
}
$this->messages = $this->createMessages($options['element']);
}
public function isTranslated($messageId, $original = false, $locale = null)
{
return array_key_exists($messageId, $this->messages);
}
public function translate($messageId, $locale = null)
{
if (array_key_exists($messageId, $this->messages)) {
return $this->messages[$messageId];
}
return $messageId;
}
protected function createMessages($element)
{
$label = $element->getLabel();
return array(
Zend_Validate_NotEmpty::IS_EMPTY => sprintf(t('%s is required and must not be empty'), $label),
WritablePathValidator::NOT_WRITABLE => sprintf(t('%s is not writable', 'config.path'), $label),
WritablePathValidator::DOES_NOT_EXIST => sprintf(t('%s does not exist', 'config.path'), $label),
ReadablePathValidator::NOT_READABLE => sprintf(t('%s is not readable', 'config.path'), $label),
DateTimeValidator::INVALID_DATETIME_FORMAT => sprintf(
t('%s not in the expected format: %%value%%'),
$label
)
);
}
protected function _loadTranslationData($data, $locale, array $options = array())
{
// nonsense, required as being abstract otherwise...
}
public function toString()
{
return 'ErrorLabeller'; // nonsense, required as being abstract otherwise...
}
}