public function renderElement($element) { if (is_string($element)) { $e = $this->form->getElement($element); } else { $e = $element; } $type = $e->getType(); $atts = $e->getAttributes(); $atts['class'] = array_get($atts, 'class') . ' ' . 'form-control'; $render = []; switch ($type) { case 'text': case 'textarea': case 'password': case 'hidden': $render[] = Form::label($e->getName(), $e->getLabel()); $render[] = Form::$type($e->getName(), $e->getValue(), $atts); break; case 'checkbox': case 'radio': break; case 'select': $render[] = Form::label($e->getName(), $e->getLabel()); $render[] = Form::select($e->getName(), (array) $e->getAttribute('options'), $e->getValue(), array_except($atts, ['options'])); break; case 'submit': $render[] = Form::submit($e->getLabel()); break; } if ($errors = $e->getMeta('errors')) { $render[] = '<p class="text-danger">' . implode('<br />', $errors) . '</p>'; } return implode("\n", $render); }
/** * Make field HTML from field array * * @param array $field * @return string */ private function makeField($field) { $output = ''; //combine additional input attributes $input_attributes = array('class' => $field['input-class']); $input_attributes = array_merge($input_attributes, $field['input-attributes']); // //TEXT if ($field['type'] == "text") { $output .= Form::text($field['name'], $field['value'], $input_attributes); } //TEXTAREA if ($field['type'] == "textarea") { $output .= Form::textarea($field['name'], $field['value'], $input_attributes); } //PASSWORD if ($field['type'] == "password") { $output .= Form::password($field['name'], $field['value'], $input_attributes); } //SELECT if ($field['type'] == "select") { $output .= Form::select($field['name'], $field['options'], $field['value'], $input_attributes); } //CHECKBOXES if ($field['type'] == "checkbox") { foreach ($field['options'] as $option_key => $option_value) { $output .= '<label class="checkbox">'; $checked = null; if (is_array($field['value']) && in_array($option_key, $field['value'])) { $checked = true; } $output .= Form::checkbox($field['name'], $option_key, $checked); $output .= $option_value; $output .= '</label>'; } } //RADIO if ($field['type'] == "radio") { foreach ($field['options'] as $option_key => $option_value) { $output .= '<label class="radio">'; $checked = false; if ($option_key == $field['value']) { $checked = true; } $output .= Form::radio($field['name'], $option_key, $checked); $output .= $option_value; $output .= '</label>'; } } //FILE if ($field['type'] == "file") { $output .= Form::file($field['name'], $input_attributes); } //return return $output; }
/** * Adds a multiselect field. It uses the chosen library. */ public static function multiselect($name, $label, $options, $selected = '', $isRequired = false, $placeholder = '', $helpText = '') { $strField = Form::select($name . '[]', $options, Input::old($name, $selected), array('multiple' => 'true', 'data-rel' => 'chosen', 'class' => 'form-control', 'data-placeholder' => $placeholder)); return self::prv_printField($name, $label, $strField, $isRequired, $helpText); }
}); Form::macro('passwordField', function ($name, $label = null, $attributes = []) { $element = Form::password($name, fieldAttributes($name, $attributes)); return fieldWrapper($name, $label, $element); }); Form::macro('textareaField', function ($name, $label = null, $value = null, $attributes = []) { $element = Form::textarea($name, $value, fieldAttributes($name, $attributes)); return fieldWrapper($name, $label, $element); }); Form::macro('selectField', function ($name, $label = null, $options, $value = null, $attributes = []) { $element = Form::select($name, $options, $value, fieldAttributes($name, $attributes)); return fieldWrapper($name, $label, $element); }); Form::macro('selectMultipleField', function ($name, $label = null, $options, $value = null, $attributes = []) { $attributes = array_merge($attributes, ['multiple' => true]); $element = Form::select($name, $options, $value, fieldAttributes($name, $attributes)); return fieldWrapper($name, $label, $element); }); Form::macro('checkboxField', function ($name, $label = null, $value = 1, $checked = null, $attributes = []) { $attributes = array_merge(['id' => 'id-field-' . $name], $attributes); $out = '<div class="checkbox'; $out .= fieldError($name) . '">'; $out .= '<label>'; $out .= Form::checkbox($name, $value, $checked, $attributes) . ' ' . $label; $out .= '</div>'; return $out; }); function fieldWrapper($name, $label, $element) { $out = '<div class="form-group'; $out .= fieldError($name) . '">';
/** * Create a HTML select element. * * @param string $name * @param string $label * @param array $options * @param string $selected * @param array $attributes * @return string */ public function select($name, $label = '', $options = array(), $selected = null, $attributes = array()) { $selected = $this->calculateValue($name, $selected); $attributes = $this->setAttributes($name, $attributes); $field = Form::select($name, $options, $selected, $attributes); return $this->buildWrapper($field, $name, $label); }