/** * Render a form <input> text element from the provided $element, * @param ElementInterface $element * @param null|string $formType * @param array $displayOptions * @return string */ public function render(ElementInterface $element, $formType = null, array $displayOptions = array()) { $formType = $this->formUtil->filterFormType($formType); $this->prepareElementBeforeRendering($element, $formType, $displayOptions); $html = parent::render($element); //Text prepend / append $escapeHelper = $this->getEscapeHtmlHelper(); $escapeAttribHelper = $this->getEscapeHtmlAttrHelper(); $prepAppClass = ''; //Prepend icon (not possible on Search forms) if ($formType != FormUtil::FORM_TYPE_SEARCH && array_key_exists('prependIcon', $displayOptions)) { $prepAppClass = $this->genUtil->addWords('input-prepend', $prepAppClass); $html = sprintf('<span class="add-on"><i class="%s"></i></span>%s', $escapeAttribHelper($displayOptions['prependIcon']), $html); } //Prepend text if ($element->getOption('prependText')) { $prepAppClass = $this->genUtil->addWords('input-prepend', $prepAppClass); $html = '<span class="add-on">' . $escapeHelper($element->getOption('prependText')) . '</span>' . $html; } //Append icon (not possible on Search forms) if ($formType != FormUtil::FORM_TYPE_SEARCH && array_key_exists('appendIcon', $displayOptions)) { $prepAppClass = $this->genUtil->addWords('input-append', $prepAppClass); $html .= sprintf('<span class="add-on"><i class="%s"></i></span>', $escapeAttribHelper($displayOptions['appendIcon'])); } //Append text if ($element->getOption('appendText')) { $prepAppClass = $this->genUtil->addWords('input-append', $prepAppClass); $html .= '<span class="add-on">' . $escapeHelper($element->getOption('appendText')) . '</span>'; } if ($prepAppClass) { $html = '<div class="' . $prepAppClass . '">' . "\n{$html}\n" . '</div>'; } return $html; }
/** * Prepares the element prior to rendering * @param \Zend\Form\ElementInterface $element * @param string $formType * @param array $displayOptions * @return void */ protected function prepareElementBeforeRendering(ElementInterface $element, $formType, array $displayOptions) { if (array_key_exists('class', $displayOptions)) { $class = $element->getAttribute('class'); $class = $this->genUtil->addWords($displayOptions['class'], $class); $escapeHtmlAttrHelper = $this->getEscapeHtmlAttrHelper(); $class = $this->genUtil->escapeWords($class, $escapeHtmlAttrHelper); $element->setAttribute('class', $class); } $this->formUtil->addIdAttributeIfMissing($element); }
/** * Render a form <select> element from the provided $element * @param ElementInterface $element * @param null|string $formType * @param array $displayOptions * @return string */ public function render(ElementInterface $element, $formType = null, array $displayOptions = array()) { if (array_key_exists('class', $displayOptions)) { $class = $element->getAttribute('class'); $class = $this->genUtil->addWords($displayOptions['class'], $class); $escapeHtmlAttrHelper = $this->getEscapeHtmlAttrHelper(); $class = $this->genUtil->escapeWords($class, $escapeHtmlAttrHelper); $element->setAttribute('class', $class); } if (array_key_exists('size', $displayOptions)) { $element->setAttribute('size', (int) $displayOptions['size']); } $this->formUtil->addIdAttributeIfMissing($element); $html = parent::render($element); return $html; }
/** * Utility form helper that renders a label (if it exists), an element, hint, description and errors * @param ElementInterface $element * @param string|null $formType * @param array $displayOptions * @param bool $renderErrors * @return string */ public function render(ElementInterface $element, $formType = null, array $displayOptions = array(), $renderErrors = true) { $formType = $this->formUtil->filterFormType($formType); $elementHelper = $this->getElementHelper(); $elementErrorsHelper = $this->getElementErrorsHelper(); $hintHelper = $this->getHintHelper(); $descriptionHelper = $this->getDescriptionHelper(); $label = (string) $element->getLabel(); $elementString = $elementHelper->render($element, $formType, $displayOptions); //Hint, description and element errors are generated only for visible elements on horizontal and vertical forms //Divs for control-group and controls are generated only for visible elements on horizontal and vertical forms, //otherwise a blank vertical space is rendered if (($formType == FormUtil::FORM_TYPE_HORIZONTAL || $formType == FormUtil::FORM_TYPE_VERTICAL) && !$element instanceof \Zend\Form\Element\Hidden && !$element instanceof \Zend\Form\Element\Csrf) { $controlGroupHelper = $this->getControlGroupHelper(); $controlGroupOpen = $controlGroupHelper->openTag($element); $controlGroupClose = $controlGroupHelper->closeTag(); $controlsHelper = $this->getControlsHelper(); $controlsOpen = $controlsHelper->openTag($element); $controlsClose = $controlsHelper->closeTag(); $hint = $hintHelper->render($element); $description = $descriptionHelper->render($element); if ($renderErrors) { $elementErrors = $elementErrorsHelper->render($element); } else { $elementErrors = ''; } } else { $controlGroupOpen = ''; $controlGroupClose = ''; //We need some whitespace between label and element on inline and search forms $controlsOpen = "\n"; $controlsClose = ''; $hint = ''; $description = ''; $elementErrors = ''; } if (!empty($label)) { //Element has a label $labelHelper = $this->getLabelHelper(); $label = $labelHelper($element, $displayOptions); } $markup = $controlGroupOpen . $label . $controlsOpen . $elementString . $hint . $description . $elementErrors . $controlsClose . $controlGroupClose; return $markup; }
/** * @param FieldsetInterface $fieldset * @param string|null $formType * @param array $displayOptions * @param bool $displayButtons Should buttons found in this fieldset be rendered? * @param bool $renderFieldsetTag Should we render the <fieldset> tag around the fieldset? * @param bool $renderErrors * @return string */ public function render(FieldsetInterface $fieldset, $formType = null, array $displayOptions = array(), $displayButtons = true, $renderFieldsetTag = true, $renderErrors = true) { $formType = $this->formUtil->filterFormType($formType); $html = ''; if ($renderFieldsetTag) { $html .= $this->openTag($fieldset, $formType, $displayOptions); } $html .= "\n" . $this->content($fieldset, $formType, $displayOptions, $displayButtons, $renderErrors); if ($renderFieldsetTag) { $html .= "\n" . $this->closeTag(); } return $html; }
/** * Generate an opening form tag * @param null|FormInterface $form * @param null|string $formType * @param array $displayOptions * @throws \DluTwBootstrap\Form\Exception\UnsupportedFormTypeException * @return string */ public function openTag(FormInterface $form = null, $formType = null, $displayOptions = array()) { $formType = $this->formUtil->filterFormType($formType); if (!array_key_exists($formType, $this->formTypeMap)) { throw new UnsupportedFormTypeException("Unsupported form type '{$formType}'."); } if ($form) { $class = $this->genUtil->addWords($this->formTypeMap[$formType], $form->getAttribute('class')); if (array_key_exists('class', $displayOptions)) { $class = $this->genUtil->addWords($displayOptions['class'], $class); } $escapeHtmlAttrHelper = $this->getEscapeHtmlAttrHelper(); $class = $this->genUtil->escapeWords($class, $escapeHtmlAttrHelper); $form->setAttribute('class', $class); } return parent::openTag($form); }
/** * Prepares the element prior to rendering * @param \Zend\Form\ElementInterface $element * @param string $formType * @param array $displayOptions * @return void */ protected function prepareElementBeforeRendering(ElementInterface $element, $formType, array $displayOptions) { $this->formUtil->addIdAttributeIfMissing($element); }
/** * Render an input element * If the passed element has no type set assumes 'text' * If the element type is not supported, returns an empty string * @param ElementInterface $element * @param null|string $formType * @param array $displayOptions * @return string */ public function render(ElementInterface $element, $formType = null, array $displayOptions = array()) { $renderer = $this->getView(); if (!method_exists($renderer, 'plugin')) { // Bail early if renderer is not pluggable return ''; } $formType = $this->formUtil->filterFormType($formType); $type = $element->getAttribute('type'); if (empty($type)) { $type = 'text'; } $type = strtolower($type); if (!isset($this->validTypes[$type])) { $type = 'text'; } //Button if ('button' == $type) { $helper = $renderer->plugin('form_button_twb'); return $helper($element, null, $formType, $displayOptions); } //Checkbox if ('checkbox' == $type) { $helper = $renderer->plugin('form_checkbox_twb'); return $helper($element, $formType, $displayOptions); } //File if ('file' == $type) { $helper = $renderer->plugin('form_file_twb'); return $helper($element, $formType, $displayOptions); } //Hidden if ('hidden' == $type) { $helper = $renderer->plugin('form_hidden_twb'); return $helper($element, $formType, $displayOptions); } //TODO - image input //Password if ('password' == $type) { $helper = $renderer->plugin('form_password_twb'); return $helper($element, $formType, $displayOptions); } //Radio if ('radio' == $type) { $helper = $renderer->plugin('form_radio_twb'); return $helper($element, $formType, $displayOptions); } //Reset if ('reset' == $type) { $helper = $renderer->plugin('form_reset_twb'); return $helper($element, $formType, $displayOptions); } //Submit if ('submit' == $type) { $helper = $renderer->plugin('form_submit_twb'); return $helper($element, $formType, $displayOptions); } //Text if ('text' == $type) { $helper = $renderer->plugin('form_text_twb'); return $helper($element, $formType, $displayOptions); } return ''; }