Esempio n. 1
0
 /**
  * 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)
 {
     // render function complains if I add the $options param, so we need to catch it like this
     $options = func_num_args() > 1 ? func_get_arg(1) : array();
     $escapeHtmlHelper = $this->getEscapeHtmlHelper();
     $labelHelper = $this->getLabelHelper();
     $elementHelper = $this->getElementHelper();
     $helpBlockHelper = $this->getHelpBlockHelper();
     $elementErrorsHelper = $this->getElementErrorsHelper();
     $label = $element->getLabel();
     $helpBlock = $element->getOption('helpblock');
     $inputErrorClass = $this->getInputErrorClass();
     // if I have a label, try and translate it
     if (isset($label) && '' !== $label) {
         // Translate the label
         if (null !== ($translator = $this->getTranslator())) {
             $label = $translator->translate($label, $this->getTranslatorTextDomain());
         }
     }
     // if element does not have and ID, create one out of it's name
     if (!$element->hasAttribute('id')) {
         $name = $element->getAttribute('name');
         if (empty($name)) {
             $name = 'input';
         }
         if (empty($this->counterName[$name])) {
             $this->counterName[$name] = 0;
         }
         $element->setAttribute('id', trim($name, "\t\n\r\v []<>{}") . '-' . $this->counterName[$name]);
         $this->counterName[$name]++;
     }
     // 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 I have a partial for this element
     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);
     }
     // render errors
     if ($this->renderErrors) {
         $elementErrors = $elementErrorsHelper->render($element);
     }
     // render element
     $elementString = $elementHelper->render($element);
     // label
     if (isset($label) && '' !== $label) {
         $label = $escapeHtmlHelper($label);
         $labelAttributes = $element->getLabelAttributes();
         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
         $type = $element->getAttribute('type');
         if ($type === 'checkbox' || $type == 'radio') {
             if (empty($labelAttributes)) {
                 $labelAttributes = array('class' => 'checkbox');
             }
             if ($labelAttributes instanceof LabelOptionsAwareInterface) {
                 $attributes = $labelAttributes->getLabelAttributes();
                 $attributes['class'] = 'checkbox' . (empty($attributes['class']) ? '' : ' ' . $attributes['class']);
                 $labelAttributes->setLabelAttributes($attributes);
             }
         }
         if ($type === 'multi_checkbox' || $type === 'radio') {
             $markup = sprintf('<label class="control-label">%s</label>%s', $label, str_replace('control-label ', '', $elementString));
         } else {
             if ($element->hasAttribute('id')) {
                 $labelOpen = '';
                 $labelClose = '';
                 if ($type === 'checkbox' || $type === 'radio') {
                     $element->setLabelAttributes($labelAttributes);
                 }
                 $label = $labelHelper($element, null, null, $options);
             } else {
                 $labelOpen = $labelHelper->openTag($labelAttributes, $options);
                 $labelClose = $labelHelper->closeTag();
             }
             //                var_dump(array($label, $labelOpen, $labelClose));
             // Button element is a special case, because label is always rendered inside it
             if ($element instanceof Button) {
                 $labelOpen = $labelClose = $label = '';
             }
             if (($type === 'checkbox' || $type === 'radio') && Form::getBootstrapType() !== Form::HORIZONTAL) {
                 $label = $labelOpen . $label . $labelClose;
                 $label = preg_replace('/(<label[^>]+>)/i', '$1%s ', $label);
                 $markup = sprintf($label, $elementString);
             } else {
                 switch ($this->labelPosition) {
                     case self::LABEL_PREPEND:
                         $markup = $labelOpen . $label . $elementString . $labelClose;
                         break;
                     case self::LABEL_APPEND:
                     default:
                         $markup = $labelOpen . $elementString . $label . $labelClose;
                         break;
                 }
             }
             //                var_dump($markup);
         }
     }
     if ($this->renderErrors) {
         $markup = (empty($markup) ? $elementString : $markup) . $elementErrors;
     }
     /*else {
           $markup = $elementString;
       }*/
     if (isset($helpBlock) && '' !== $helpBlock) {
         $markup .= $helpBlockHelper->openTag($element->getOption('helpblock_attributes'), $element->getOption('helpblock_mode')) . $helpBlock . $helpBlockHelper->closeTag();
     }
     // if type is horizontal or vertical, start wrapping
     if (Form::getBootstrapType() === Form::VERTICAL) {
         $markup = sprintf('<div class="control-group%s"><div class="controls">%s</div></div>', count($element->getMessages()) ? ' error' : '', $markup);
     } elseif (Form::getBootstrapType() === Form::HORIZONTAL) {
         $pos = strpos($markup, '</label');
         if ($pos !== FALSE && $pos > 0) {
             $pos += 8;
             $markup2 = substr($markup, 0, $pos) . '<div class="controls">' . substr($markup, $pos) . '</div>';
             $markup = sprintf('<div class="control-group%s">%s</div>', count($element->getMessages()) ? ' error' : '', $markup2);
         }
     }
     return $markup;
 }