Ejemplo n.º 1
0
 /**
  * Renders a form element label from an element
  * @param \Zend\Form\ElementInterface $element
  * @param array|null $displayOptions
  * @return string
  * @throws \Zend\Form\Exception\DomainException
  */
 public function render(ElementInterface $element, array $displayOptions = array())
 {
     $labelContent = $element->getLabel();
     if (empty($labelContent)) {
         throw new DomainException(sprintf('%s: No label set in the element.', __METHOD__));
     }
     //Translate
     if (null !== ($translator = $this->getTranslator())) {
         $labelContent = $translator->translate($labelContent, $this->getTranslatorTextDomain());
     }
     //Escape
     $escaperHtml = $this->getEscapeHtmlHelper();
     $labelContent = $escaperHtml($labelContent);
     //Label attributes
     $labelAttributes = $element->getLabelAttributes();
     if (empty($labelAttributes)) {
         $labelAttributes = array();
     }
     $labelAttributes = $this->genUtil->addWordsToArrayItem('control-label', $labelAttributes, 'class');
     if (array_key_exists('required', $displayOptions) && $displayOptions['required']) {
         $labelAttributes = $this->genUtil->addWordsToArrayItem('required', $labelAttributes, 'class');
     }
     if (!array_key_exists('id', $labelAttributes)) {
         $labelAttributes['id'] = 'label-' . $element->getName();
     }
     $element->setLabelAttributes($labelAttributes);
     $formLabelHelper = $this->formLabelHelper;
     return $formLabelHelper($element, $labelContent);
 }
Ejemplo n.º 2
0
 /**
  * Generate an opening label tag
  * @param  null|array|ElementInterface $attributesOrElement
  * @return string
  */
 public function openTag($attributesOrElement = null)
 {
     $labelAttributes = array();
     if ($attributesOrElement instanceof LabelOptionsAwareInterface) {
         $labelAttributes = $attributesOrElement->getLabelAttributes();
     }
     $labelAttributes['class'] = 'btn' . (empty($labelAttributes['class']) ? '' : ' ' . $labelAttributes['class']);
     return parent::openTag($labelAttributes);
 }
Ejemplo n.º 3
0
 /**
  * @param ElementInterface $element
  *
  * @return string
  */
 protected function renderLabel(ElementInterface $element)
 {
     $labelPlugin = $this->getView()->plugin('sxb_form_control_label');
     $label = $element->getLabel();
     if (null !== $label) {
         $label = $labelPlugin($label, $element->getName(), $element->getLabelAttributes());
     }
     return $label;
 }
 /**
  * @see \Zend\Form\View\Helper\FormMultiCheckbox::render()
  * @param \Zend\Form\ElementInterface $oElement
  * @return string
  */
 public function render(\Zend\Form\ElementInterface $oElement)
 {
     $aElementOptions = $oElement->getOptions();
     $sCheckboxClass = isset($aElementOptions['inline']) && $aElementOptions['inline'] == false ? 'checkbox' : 'checkbox-inline';
     $aLabelAttributes = $oElement->getLabelAttributes();
     if (empty($aLabelAttributes['class'])) {
         $aLabelAttributes['class'] = $sCheckboxClass;
     } elseif (!preg_match('/(\\s|^)' . $sCheckboxClass . '(\\s|$)/', $aLabelAttributes['class'])) {
         $aLabelAttributes['class'] .= ' ' . $sCheckboxClass;
     }
     $oElement->setLabelAttributes($aLabelAttributes);
     return parent::render($oElement);
 }
 /**
  * @see \Zend\Form\View\Helper\FormCheckbox::render()
  * @param \Zend\Form\ElementInterface $oElement
  * @throws \LogicException
  * @throws \InvalidArgumentException
  * @return string
  */
 public function render(\Zend\Form\ElementInterface $oElement)
 {
     if (!$oElement instanceof \Zend\Form\Element\Checkbox) {
         throw new \InvalidArgumentException(sprintf('%s requires that the element is of type Zend\\Form\\Element\\Checkbox', __METHOD__));
     }
     if (($sName = $oElement->getName()) !== 0 && empty($sName)) {
         throw new \LogicException(sprintf('%s requires that the element has an assigned name; none discovered', __METHOD__));
     }
     $aAttributes = $oElement->getAttributes();
     $aAttributes['name'] = $sName;
     $aAttributes['type'] = $this->getInputType();
     $aAttributes['value'] = $oElement->getCheckedValue();
     $sClosingBracket = $this->getInlineClosingBracket();
     if ($oElement->isChecked()) {
         $aAttributes['checked'] = 'checked';
     }
     //Render label
     $sLabelOpen = $sLabelClose = $sLabelContent = '';
     //Render label and visible element
     if ($sLabelContent = $oElement->getLabel()) {
         if ($oTranslator = $this->getTranslator()) {
             $sLabelContent = $oTranslator->translate($sLabelContent, $this->getTranslatorTextDomain());
         }
         $oLabelHelper = $this->getLabelHelper();
         $sLabelOpen = $oLabelHelper->openTag($oElement->getLabelAttributes() ?: null);
         $sLabelClose = $oLabelHelper->closeTag();
     }
     //Render checkbox
     $sElementContent = sprintf('<input %s%s', $this->createAttributesString($aAttributes), $sClosingBracket);
     //Add label markup
     if ($this->getLabelPosition($oElement) === \Zend\Form\View\Helper\FormRow::LABEL_PREPEND) {
         $sElementContent = $sLabelOpen . ($sLabelContent ? rtrim($sLabelContent) . ' ' : '') . $sElementContent . $sLabelClose;
     } else {
         $sElementContent = $sLabelOpen . $sElementContent . ($sLabelContent ? ' ' . ltrim($sLabelContent) : '') . $sLabelClose;
     }
     //Render hidden input
     if ($oElement->useHiddenElement()) {
         $sElementContent = sprintf('<input type="hidden" %s%s', $this->createAttributesString(array('name' => $aAttributes['name'], 'value' => $oElement->getUncheckedValue())), $sClosingBracket) . $sElementContent;
     }
     return $sElementContent;
 }
Ejemplo n.º 6
0
 /**
  * render
  *
  * Return the HTML representation of the form element's label.
  *
  * @param ElementInterface  $element  The form element which label should be rendered.
  * @param array             $options  Optional view helper options.
  *
  * @return string
  */
 public function render(ElementInterface $element, array $options = [])
 {
     if (!empty($options)) {
         $this->setOptions($options);
     }
     $name = $element->getName();
     $label = $element->getLabel();
     if (empty($label) && false == $this->getOption('allow_empty_label', false)) {
         return '';
     }
     // Merge the label attributes
     $attributes = $this->getAttributes();
     if ($element instanceof LabelAwareInterface) {
         $attributes = array_merge($attributes, $element->getLabelAttributes());
     }
     if (!empty($name) && !isset($attributes['for'])) {
         $attributes['for'] = $name;
     }
     $this->setAttributes(array_unique($attributes));
     return parent::render($label);
 }
Ejemplo n.º 7
0
 /**
  * Utility form helper that renders a label (if it exists), an element and errors
  *
  * @param ElementInterface $element
  * @return string
  * @throws \Zend\Form\Exception\DomainException
  */
 public function render(ElementInterface $element, $ignoreViewPartial = false)
 {
     $labelAttributes = $element->getLabelAttributes();
     $labelWidth = $element->getOption('labelWidth');
     if (!$labelWidth) {
         $labelWidth = $this->labelSpanWidth;
     }
     if ($element instanceof ViewPartialProviderInterface && !$ignoreViewPartial) {
         return $this->getView()->partial($element->getViewPartial(), array('element' => $element, 'labelWitdh' => $labelWidth, 'label_attributes' => $labelAttributes));
     }
     $escapeHtmlHelper = $this->getEscapeHtmlHelper();
     $labelHelper = $this->getLabelHelper();
     $elementHelper = $this->getElementHelper();
     $elementErrorsHelper = $this->getElementErrorsHelper();
     $inputErrorClass = $this->getInputErrorClass();
     $elementErrors = $elementErrorsHelper->render($element);
     // general Class
     $form_row_class = 'row';
     if ($this->layout == Form::LAYOUT_HORIZONTAL) {
         $form_row_class = 'form-group';
     }
     // Does this element have errors ?
     if (!empty($elementErrors) && !empty($inputErrorClass) && $this->layout != Form::LAYOUT_BARE) {
         $classAttributes = $element->hasAttribute('class') ? $element->getAttribute('class') . ' ' : '';
         $classAttributes = $classAttributes . $inputErrorClass;
         $element->setAttribute('class', $classAttributes);
     }
     if (!$element->hasAttribute('id')) {
         $elementId = preg_replace(array('~[^A-Za-z0-9_-]~', '~--+~', '~^-|-$~'), array('-', '-', ''), $element->getName());
         $element->setAttribute('id', $elementId);
     } else {
         $elementId = $element->getAttribute('id');
     }
     /*
      * add form-control class to all form elements, but "submit" or "reset" and Buttons!
      */
     if ($element->getAttribute('type') != 'submit' && $element->getAttribute('type') != 'reset' && $element->getAttribute('type') != 'checkbox' && !$element instanceof Button) {
         $element->setAttribute('class', $element->getAttribute('class') . ' form-control ');
     }
     $elementString = $elementHelper->render($element);
     $desc = $element->getOption('description', false);
     if ($desc && $this->layout != Form::LAYOUT_BARE) {
         if (null !== ($translator = $this->getTranslator())) {
             $desc = $translator->translate($desc, $this->getTranslatorTextDomain());
         }
         $elementString .= sprintf('<div id="%s-desc" class="cam-description alert alert-info">%s</div>', $elementId, $desc);
     }
     if (!$element instanceof \Zend\Form\Element\Hidden && !$element instanceof \Zend\Form\Element\Button && $this->layout != Form::LAYOUT_BARE) {
         $elementString .= sprintf('<div id="%s-errors" class="errors">%s</div>', $elementId, $elementErrors);
     }
     // moved label here so we can change it in the ElementViewHelper
     $label = $element->getLabel();
     if (isset($label) && '' !== $label && !$element instanceof \Zend\Form\Element\Button) {
         // Translate the label
         if (null !== ($translator = $this->getTranslator())) {
             $label = $translator->translate($label, $this->getTranslatorTextDomain());
         }
         $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 === 'multi_checkbox' || $type === 'radio') {
             $markup = sprintf('<fieldset><legend>%s</legend>%s</fieldset>', $label, $elementString);
         } else {
             if ($this->layout == Form::LAYOUT_BARE) {
                 $markup = $elementString;
             } else {
                 if (!isset($labelAttributes['for'])) {
                     $labelAttributes['for'] = $elementId;
                 }
                 if (Form::LAYOUT_HORIZONTAL == $this->layout) {
                     if (!isset($labelAttributes['class'])) {
                         $labelAttributes['class'] = '';
                     }
                     $labelAttributes['class'] .= ' control-label';
                 }
                 $element->setLabelAttributes($labelAttributes);
                 $label = $labelHelper($element);
                 if ($this->shouldWrap) {
                     $spanWidth = 12 - $labelWidth;
                     $elementString = sprintf('<div class="col-md-%d%s" id="' . $elementId . '-span">%s</div>', $spanWidth, $elementErrors ? " {$inputErrorClass}" : '', $elementString);
                     $label = sprintf('<div class="col-md-%d yk-label">%s</div>', $labelWidth, $label);
                 }
                 $markup = $label . $elementString;
             }
         }
     } else {
         if ($this->shouldWrap && !$element instanceof \Zend\Form\Element\Hidden && !$element instanceof \Zend\Form\Element\Button && $this->layout != Form::LAYOUT_BARE) {
             $elementString = sprintf('<div class="col-md-12">%s</div>', $elementString);
         }
         $markup = $elementString;
     }
     if ($this->shouldWrap && !$element instanceof \Zend\Form\Element\Hidden && !$element instanceof \Zend\Form\Element\Button && $this->layout != Form::LAYOUT_BARE) {
         $markup = sprintf('<div class="controls controls-row ' . $form_row_class . '">%s</div>', $markup);
     }
     return $markup;
 }
Ejemplo n.º 8
0
 /**
  * Render a form <input> element from the provided $element
  *
  * @param  ElementInterface $element
  * @throws Exception\InvalidArgumentException
  * @throws Exception\DomainException
  * @return string
  */
 public function render(ElementInterface $element)
 {
     if (!$element instanceof CheckboxElement) {
         throw new Exception\InvalidArgumentException(sprintf('%s requires that the element is of type' . ' Zend\\Form\\Element\\Checkbox', __METHOD__));
     }
     $name = $element->getName();
     if (empty($name) && $name !== 0) {
         throw new Exception\DomainException(sprintf('%s requires that the element has an assigned name;' . ' none discovered', __METHOD__));
     }
     $attributes = $element->getAttributes();
     $attributes['name'] = $name;
     $attributes['type'] = $this->getInputType();
     $attributes['value'] = $element->getCheckedValue();
     $closingBracket = $this->getInlineClosingBracket();
     if ($element->isChecked()) {
         $attributes['checked'] = 'checked';
     }
     $input = sprintf('<input %s%s', $this->createAttributesString($attributes), $closingBracket);
     $escapeHtmlHelper = $this->getEscapeHtmlHelper();
     $labelHelper = $this->getLabelHelper();
     $labelClose = $labelHelper->closeTag();
     $labelPosition = $this->getLabelPosition();
     $globalLabelAttributes = $element->getLabelAttributes();
     if (empty($globalLabelAttributes)) {
         $globalLabelAttributes = $this->labelAttributes;
     }
     $label = $element->getAttribute('description') ?: $element->getLabel();
     $labelAttributes = $this->labelAttributes ?: $element->getLabelAttributes();
     if (null !== ($translator = $this->getTranslator())) {
         $label = $translator->translate($label, $this->getTranslatorTextDomain());
     }
     $label = $escapeHtmlHelper($label);
     $labelOpen = $labelHelper->openTag($labelAttributes);
     $template = $labelOpen . '%s%s' . $labelClose;
     switch ($labelPosition) {
         case static::LABEL_PREPEND:
             $rendered = sprintf($template, $label, $input);
             break;
         case static::LABEL_APPEND:
         default:
             $rendered = sprintf($template, $input, $label);
             break;
     }
     // Render hidden element
     $useHiddenElement = null !== $this->useHiddenElement ? $this->useHiddenElement : (method_exists($element, 'useHiddenElement') ? $element->useHiddenElement() : false);
     if ($useHiddenElement) {
         $hiddenAttributes = array('name' => $attributes['name'], 'value' => $element->getUncheckedValue());
         $rendered = sprintf('<input type="hidden" %s%s', $this->createAttributesString($hiddenAttributes), $closingBracket) . $rendered;
     }
     return $rendered;
 }
Ejemplo n.º 9
0
 /**
  * Render options
  *
  * @param ElementInterface $element
  * @param array            $options
  * @param array            $selectedOptions
  * @param array            $attributes
  * @return string
  */
 protected function renderOptions(ElementInterface $element, array $options, array $selectedOptions, array $attributes)
 {
     $escapeHtmlHelper = $this->getEscapeHtmlHelper();
     $labelHelper = $this->getLabelHelper();
     $labelClose = $labelHelper->closeTag();
     $labelPosition = $this->getLabelPosition();
     $globalLabelAttributes = $element->getLabelAttributes();
     $closingBracket = $this->getInlineClosingBracket();
     if (empty($globalLabelAttributes)) {
         $globalLabelAttributes = $this->labelAttributes;
     }
     $combinedMarkup = array();
     $count = 0;
     foreach ($options as $key => $optionSpec) {
         $count++;
         if ($count > 1 && array_key_exists('id', $attributes)) {
             unset($attributes['id']);
         }
         $value = '';
         $label = $key;
         $selected = false;
         $disabled = false;
         $inputAttributes = $attributes;
         $labelAttributes = $globalLabelAttributes;
         if (is_string($optionSpec) || is_numeric($optionSpec) || is_bool($optionSpec)) {
             $optionSpec = array('value' => (string) $optionSpec);
         }
         if (isset($optionSpec['value'])) {
             $value = $optionSpec['value'];
         }
         if (isset($optionSpec['label'])) {
             $label = $optionSpec['label'];
         }
         if (isset($optionSpec['selected'])) {
             $selected = $optionSpec['selected'];
         }
         if (isset($optionSpec['disabled'])) {
             $disabled = $optionSpec['disabled'];
         }
         if (isset($optionSpec['label_attributes'])) {
             $labelAttributes = isset($labelAttributes) ? array_merge($labelAttributes, $optionSpec['label_attributes']) : $optionSpec['label_attributes'];
         }
         if (isset($optionSpec['attributes'])) {
             $inputAttributes = array_merge($inputAttributes, $optionSpec['attributes']);
         }
         if (in_array($value, $selectedOptions, true)) {
             $selected = true;
         }
         $inputAttributes['value'] = $value;
         $inputAttributes['checked'] = $selected;
         $inputAttributes['disabled'] = $disabled;
         $input = sprintf('<input %s%s', $this->createAttributesString($inputAttributes), $closingBracket);
         $label = $escapeHtmlHelper($label);
         $labelOpen = $labelHelper->openTag($labelAttributes);
         $template = $labelOpen . '%s%s' . $labelClose;
         switch ($labelPosition) {
             case self::LABEL_PREPEND:
                 $markup = sprintf($template, $label, $input);
                 break;
             case self::LABEL_APPEND:
             default:
                 $markup = sprintf($template, $input, $label);
                 break;
         }
         $combinedMarkup[] = $markup;
     }
     return implode($this->getSeparator(), $combinedMarkup);
 }
Ejemplo n.º 10
0
 /**
  * @param ElementInterface $element
  * @return array
  */
 protected function getLabelAttributesByElement($element)
 {
     $labelAttributes = $element->getLabelAttributes();
     if (empty($labelAttributes)) {
         $labelAttributes = array();
     }
     if (!isset($labelAttributes['for'])) {
         $labelAttributes['for'] = $element->getAttribute('id');
     }
     if (isset($labelAttributes['class'])) {
         $labelAttributes['class'] .= ' ';
     } else {
         $labelAttributes['class'] = '';
     }
     if (!isset($labelAttributes['size'])) {
         $size = $element->getOption('size') ? 2 : 0;
     } else {
         $size = $labelAttributes['size'];
         unset($labelAttributes['size']);
     }
     $labelAttributes['class'] .= 'control-label';
     if (!empty($size)) {
         $labelAttributes['class'] .= sprintf(' col-lg-%s col-md-%s col-sm-%s col-xs-%s', $size, $size, $size, $size);
     }
     return $labelAttributes;
 }