Source of file FormRow.php
Size: 12,152 Bytes - Last Modified: 2014-03-12T23:21:18+01:00
/home/theseer/Downloads/ZendFramework-2.3.0/library/Zend/Form/View/Helper/FormRow.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436 | <?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 Zend\Form\Element\Button; use Zend\Form\Element\MonthSelect; use Zend\Form\ElementInterface; use Zend\Form\Exception; use Zend\Form\LabelAwareInterface; class FormRow extends AbstractHelper { const LABEL_APPEND = 'append'; const LABEL_PREPEND = 'prepend'; /** * The class that is added to element that have errors * * @var string */ protected $inputErrorClass = 'input-error'; /** * The attributes for the row label * * @var array */ protected $labelAttributes; /** * Where will be label rendered? * * @var string */ protected $labelPosition = self::LABEL_PREPEND; /** * Are the errors are rendered by this helper? * * @var bool */ protected $renderErrors = true; /** * Form label helper instance * * @var FormLabel */ protected $labelHelper; /** * Form element helper instance * * @var FormElement */ protected $elementHelper; /** * Form element errors helper instance * * @var FormElementErrors */ protected $elementErrorsHelper; /** * @var string */ protected $partial; /** * Invoke helper as functor * * Proxies to {@link render()}. * * @param null|ElementInterface $element * @param null|string $labelPosition * @param bool $renderErrors * @param string|null $partial * @return string|FormRow */ public function __invoke(ElementInterface $element = null, $labelPosition = null, $renderErrors = null, $partial = null) { if (!$element) { return $this; } if ($labelPosition !== null) { $this->setLabelPosition($labelPosition); } elseif ($this->labelPosition === null) { $this->setLabelPosition(self::LABEL_PREPEND); } if ($renderErrors !== null) { $this->setRenderErrors($renderErrors); } if ($partial !== null) { $this->setPartial($partial); } return $this->render($element); } /** * Utility form helper that renders a label (if it exists), an element and errors * * @param ElementInterface $element * @throws \Zend\Form\Exception\DomainException * @return string */ public function render(ElementInterface $element) { $escapeHtmlHelper = $this->getEscapeHtmlHelper(); $labelHelper = $this->getLabelHelper(); $elementHelper = $this->getElementHelper(); $elementErrorsHelper = $this->getElementErrorsHelper(); $label = $element->getLabel(); $inputErrorClass = $this->getInputErrorClass(); if (isset($label) && '' !== $label) { // Translate the label if (null !== ($translator = $this->getTranslator())) { $label = $translator->translate( $label, $this->getTranslatorTextDomain() ); } } // Does this element have errors ? if (count($element->getMessages()) > 0 && !empty($inputErrorClass)) { $classAttributes = ($element->hasAttribute('class') ? $element->getAttribute('class') . ' ' : ''); $classAttributes = $classAttributes . $inputErrorClass; $element->setAttribute('class', $classAttributes); } if ($this->partial) { $vars = array( 'element' => $element, 'label' => $label, 'labelAttributes' => $this->labelAttributes, 'labelPosition' => $this->labelPosition, 'renderErrors' => $this->renderErrors, ); return $this->view->render($this->partial, $vars); } if ($this->renderErrors) { $elementErrors = $elementErrorsHelper->render($element); } $elementString = $elementHelper->render($element); // hidden elements do not need a <label> -https://github.com/zendframework/zf2/issues/5607 $type = $element->getAttribute('type'); if (isset($label) && '' !== $label && $type !== 'hidden') { $labelAttributes = array(); if ($element instanceof LabelAwareInterface) { $labelAttributes = $element->getLabelAttributes(); } if (! $element instanceof LabelAwareInterface || ! $element->getLabelOption('disable_html_escape')) { $label = $escapeHtmlHelper($label); } if (empty($labelAttributes)) { $labelAttributes = $this->labelAttributes; } // Multicheckbox elements have to be handled differently as the HTML standard does not allow nested // labels. The semantic way is to group them inside a fieldset if ($type === 'multi_checkbox' || $type === 'radio' || $element instanceof MonthSelect ) { $markup = sprintf( '<fieldset><legend>%s</legend>%s</fieldset>', $label, $elementString); } else { // Ensure element and label will be separated if element has an `id`-attribute. // If element has label option `always_wrap` it will be nested in any case. if ($element->hasAttribute('id') && ($element instanceof LabelAwareInterface && !$element->getLabelOption('always_wrap')) ) { $labelOpen = ''; $labelClose = ''; $label = $labelHelper($element); } else { $labelOpen = $labelHelper->openTag($labelAttributes); $labelClose = $labelHelper->closeTag(); } if ($label !== '' && (!$element->hasAttribute('id')) || ($element instanceof LabelAwareInterface && $element->getLabelOption('always_wrap')) ) { $label = '<span>' . $label . '</span>'; } // Button element is a special case, because label is always rendered inside it if ($element instanceof Button) { $labelOpen = $labelClose = $label = ''; } switch ($this->labelPosition) { case self::LABEL_PREPEND: $markup = $labelOpen . $label . $elementString . $labelClose; break; case self::LABEL_APPEND: default: $markup = $labelOpen . $elementString . $label . $labelClose; break; } } if ($this->renderErrors) { $markup .= $elementErrors; } } else { if ($this->renderErrors) { $markup = $elementString . $elementErrors; } else { $markup = $elementString; } } return $markup; } /** * Set the class that is added to element that have errors * * @param string $inputErrorClass * @return FormRow */ public function setInputErrorClass($inputErrorClass) { $this->inputErrorClass = $inputErrorClass; return $this; } /** * Get the class that is added to element that have errors * * @return string */ public function getInputErrorClass() { return $this->inputErrorClass; } /** * Set the attributes for the row label * * @param array $labelAttributes * @return FormRow */ public function setLabelAttributes($labelAttributes) { $this->labelAttributes = $labelAttributes; return $this; } /** * Get the attributes for the row label * * @return array */ public function getLabelAttributes() { return $this->labelAttributes; } /** * Set the label position * * @param string $labelPosition * @throws \Zend\Form\Exception\InvalidArgumentException * @return FormRow */ public function setLabelPosition($labelPosition) { $labelPosition = strtolower($labelPosition); if (!in_array($labelPosition, array(self::LABEL_APPEND, self::LABEL_PREPEND))) { throw new Exception\InvalidArgumentException(sprintf( '%s expects either %s::LABEL_APPEND or %s::LABEL_PREPEND; received "%s"', __METHOD__, __CLASS__, __CLASS__, (string) $labelPosition )); } $this->labelPosition = $labelPosition; return $this; } /** * Get the label position * * @return string */ public function getLabelPosition() { return $this->labelPosition; } /** * Set if the errors are rendered by this helper * * @param bool $renderErrors * @return FormRow */ public function setRenderErrors($renderErrors) { $this->renderErrors = (bool) $renderErrors; return $this; } /** * Retrieve if the errors are rendered by this helper * * @return bool */ public function getRenderErrors() { return $this->renderErrors; } /** * Set a partial view script to use for rendering the row * * @param null|string $partial * @return FormRow */ public function setPartial($partial) { $this->partial = $partial; return $this; } /** * Retrieve current partial * * @return null|string */ public function getPartial() { return $this->partial; } /** * Retrieve the FormLabel helper * * @return FormLabel */ protected function getLabelHelper() { if ($this->labelHelper) { return $this->labelHelper; } if (method_exists($this->view, 'plugin')) { $this->labelHelper = $this->view->plugin('form_label'); } if (!$this->labelHelper instanceof FormLabel) { $this->labelHelper = new FormLabel(); } if ($this->hasTranslator()) { $this->labelHelper->setTranslator( $this->getTranslator(), $this->getTranslatorTextDomain() ); } return $this->labelHelper; } /** * Retrieve the FormElement helper * * @return FormElement */ protected function getElementHelper() { if ($this->elementHelper) { return $this->elementHelper; } if (method_exists($this->view, 'plugin')) { $this->elementHelper = $this->view->plugin('form_element'); } if (!$this->elementHelper instanceof FormElement) { $this->elementHelper = new FormElement(); } return $this->elementHelper; } /** * Retrieve the FormElementErrors helper * * @return FormElementErrors */ protected function getElementErrorsHelper() { if ($this->elementErrorsHelper) { return $this->elementErrorsHelper; } if (method_exists($this->view, 'plugin')) { $this->elementErrorsHelper = $this->view->plugin('form_element_errors'); } if (!$this->elementErrorsHelper instanceof FormElementErrors) { $this->elementErrorsHelper = new FormElementErrors(); } return $this->elementErrorsHelper; } } |