/** * Render select * * @param \FrenchFrogs\Form\Element\Select $element * @return string */ public function select(Form\Element\Select $element) { // CLASS $class = Style::FORM_GROUP_CLASS; // ERROR if ($hasError = !$element->getValidator()->isValid()) { $element->addClass('form-error'); if (empty($element->getAttribute('data-placement'))) { $element->addAttribute('data-placement', 'bottom'); } $message = ''; foreach ($element->getValidator()->getErrors() as $error) { $message .= $error . ' '; } $element->addAttribute('data-original-title', $message); $class .= ' ' . Style::FORM_GROUP_ERROR; } // LABEL $label = ''; if ($element->getForm()->hasLabel()) { $label = '<label for="' . $element->getName() . '" class="col-md-3 control-label">' . $element->getLabel() . ($element->hasRule('required') ? ' *' : '') . '</label>'; } // OPTIONS $options = ''; if ($element->hasPlaceholder()) { $options .= html('option', ['value' => null], $element->getPlaceholder()); } $elementValue = (array) $element->getValue(); foreach ($element->getOptions() as $value => $key) { $attr = ['value' => $value]; if ($element->hasValue() && in_array($value, $elementValue)) { $attr['selected'] = 'selected'; } $options .= html('option', $attr, $key); } // INPUT $element->addClass(Style::FORM_ELEMENT_CONTROL); $element->addAttribute('id', $element->getName()); if ($element->isMultiple()) { $element->setName($element->getName() . '[]'); } $html = html('select', $element->getAttributes(), $options); // DESCRIPTION if ($element->hasDescription()) { $html .= html('span', ['class' => 'help-block'], $element->getDescription()); } $html = html('div', ['class' => 'col-md-9'], $html); // FINAL CONTAINER return html('div', compact('class'), $label . $html) . PHP_EOL; }
public function select(Form\Element\Select $element) { if ($hasError = !$element->getValidator()->isValid()) { $element->removeAttribute('data-trigger'); //Si il y avait un popoover au départ on le surcharge par un tooltip $element->addClass('form-error'); if (empty($element->getAttribute('data-placement'))) { $element->addAttribute('data-placement', 'bottom'); } $message = ''; foreach ($element->getValidator()->getErrors() as $error) { $message .= $error . ' '; } $element->addAttribute('data-original-title', $message); $element->addAttribute('data-toggle', 'tooltip'); } $element->addClass(Style::FORM_ELEMENT_CONTROL); $label = ''; if ($element->getForm()->hasLabel()) { $label = '<label for="' . $element->getName() . '">' . $element->getLabel() . ($element->hasRule('required') ? ' *' : '') . '</label>'; } $class = Style::FORM_GROUP_CLASS; if ($hasError) { $class .= ' ' . Style::FORM_GROUP_ERROR; } $options = ''; if ($element->hasPlaceholder()) { $options .= html('option', ['value' => null], $element->getPlaceholder()); } $elementValue = (array) $element->getValue(); foreach ($element->getOptions() as $value => $key) { $attr = ['value' => $value]; if ($element->hasValue() && in_array($value, $elementValue)) { $attr['selected'] = 'selected'; } $options .= html('option', $attr, $key); } if ($element->isMultiple()) { $element->setName($element->getName() . '[]'); } $html = $label . html('select', $element->getAttributes(), $options); $html = html('div', compact('class'), $html); if ($element->isMultiple()) { $element->setName(chop($element->getName(), '[]')); } return $html; }