Source of file FormElementErrors.php
Size: 5,105 Bytes - Last Modified: 2014-03-12T23:21:18+01:00
/home/theseer/Downloads/ZendFramework-2.3.0/library/Zend/Form/View/Helper/FormElementErrors.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185 | <?php /** * Zend Framework (http://framework.zend.com/) * * @link http://github.com/zendframework/zf2 for the canonical source repository * @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License */ namespace Zend\Form\View\Helper; use Traversable; use Zend\Form\ElementInterface; use Zend\Form\Exception; class FormElementErrors extends AbstractHelper { /**@+ * @var string Templates for the open/close/separators for message tags */ protected $messageCloseString = '</li></ul>'; protected $messageOpenFormat = '<ul%s><li>'; protected $messageSeparatorString = '</li><li>'; /**@-*/ /** * @var array Default attributes for the open format tag */ protected $attributes = array(); /** * Invoke helper as functor * * Proxies to {@link render()} if an element is passed. * * @param ElementInterface $element * @param array $attributes * @return string|FormElementErrors */ public function __invoke(ElementInterface $element = null, array $attributes = array()) { if (!$element) { return $this; } return $this->render($element, $attributes); } /** * Render validation errors for the provided $element * * @param ElementInterface $element * @param array $attributes * @throws Exception\DomainException * @return string */ public function render(ElementInterface $element, array $attributes = array()) { $messages = $element->getMessages(); if (empty($messages)) { return ''; } if (!is_array($messages) && !$messages instanceof Traversable) { 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)) )); } // 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 = array(); array_walk_recursive($messages, function ($item) use (&$messagesToPrint, $escapeHtml) { $messagesToPrint[] = $escapeHtml($item); }); if (empty($messagesToPrint)) { return ''; } // Generate markup $markup = sprintf($this->getMessageOpenFormat(), $attributes); $markup .= implode($this->getMessageSeparatorString(), $messagesToPrint); $markup .= $this->getMessageCloseString(); return $markup; } /** * Set the attributes that will go on the message open format * * @param array $attributes key value pairs of attributes * @return FormElementErrors */ public function setAttributes(array $attributes) { $this->attributes = $attributes; return $this; } /** * Get the attributes that will go on the message open format * * @return array */ public function getAttributes() { return $this->attributes; } /** * Set the string used to close message representation * * @param string $messageCloseString * @return FormElementErrors */ public function setMessageCloseString($messageCloseString) { $this->messageCloseString = (string) $messageCloseString; return $this; } /** * Get the string used to close message representation * * @return string */ public function getMessageCloseString() { return $this->messageCloseString; } /** * Set the formatted string used to open message representation * * @param string $messageOpenFormat * @return FormElementErrors */ public function setMessageOpenFormat($messageOpenFormat) { $this->messageOpenFormat = (string) $messageOpenFormat; return $this; } /** * Get the formatted string used to open message representation * * @return string */ public function getMessageOpenFormat() { return $this->messageOpenFormat; } /** * Set the string used to separate messages * * @param string $messageSeparatorString * @return FormElementErrors */ public function setMessageSeparatorString($messageSeparatorString) { $this->messageSeparatorString = (string) $messageSeparatorString; return $this; } /** * Get the string used to separate messages * * @return string */ public function getMessageSeparatorString() { return $this->messageSeparatorString; } } |