Example #1
0
 /**
  * Render checkbox multi
  *
  * @param \FrenchFrogs\Form\Element\Checkbox $element
  * @return string
  */
 public function checkbox(Form\Element\Checkbox $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
     $elementLabel = '';
     if ($element->getForm()->hasLabel()) {
         $elementLabel = '<label for="' . $element->getName() . '[]" class="col-md-3 control-label">' . $element->getLabel() . ($element->hasRule('required') ? ' *' : '') . '</label>';
     }
     // OPTIONS
     $options = '';
     foreach ($element->getOptions() as $value => $label) {
         $opt = '';
         // INPUT
         $attr = ['type' => 'checkbox', 'name' => $element->getName() . '[]', 'value' => $value];
         // VALUE
         $values = (array) $element->getValue();
         if (!is_null($element->getValue()) && in_array($value, $values)) {
             $attr['checked'] = 'checked';
         }
         $opt .= html('input', $attr);
         $opt .= $label;
         $options .= '<label>' . $opt . '</label>';
     }
     // DESCRIPTION
     if ($element->hasDescription()) {
         $options .= html('span', ['class' => 'help-block'], $element->getDescription());
     }
     // INPUT
     $element->addClass('checkbox-list');
     $html = html('div', $element->getAttributes(), $options);
     // FINAL CONTAINER
     $html = html('div', ['class' => 'col-md-9'], $html);
     return html('div', compact('class'), $elementLabel . $html);
 }
Example #2
0
 public function checkbox(Form\Element\Checkbox $element)
 {
     // error
     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');
     }
     $html = '<label for="' . $element->getName() . '[]">' . $element->getLabel() . '</label>';
     $options = '';
     foreach ($element->getOptions() as $value => $label) {
         $options .= '<label class="' . Style::FORM_ELEMENT_CHECKBOX_INLINE . '">';
         $attr = ['type' => 'checkbox', 'name' => $element->getName() . '[]', 'value' => $value];
         // value
         if (!is_null($element->getValue()) && in_array($value, $element->getValue())) {
             $attr['checked'] = 'checked';
         }
         $options .= html('input', $attr);
         $options .= $label;
         $options .= '</label>';
     }
     $html .= html('div', $element->getAttributes(), $options);
     $class = Style::FORM_GROUP_CLASS;
     if ($hasError) {
         $class .= ' ' . Style::FORM_GROUP_ERROR;
     }
     $html = html('div', compact('class'), $html);
     return $html;
 }