/** * @param OptionsField $field * @return string */ public function render(OptionsField $field) { $required = $field->hasRule('ch\\metanet\\formHandler\\rule\\RequiredRule') === true ? ' aria-required="true"' : null; $optionsCount = count($field->getOptions()); $fieldValue = (array) $field->getValue(); $html = '<ul' . $this->getAttributesAsHtml() . '>'; $fieldName = $optionsCount === 1 ? $field->getName() : $field->getName() . '[]'; foreach ($field->getOptions() as $key => $val) { $attrId = ' id="' . $field->getName() . ($optionsCount !== 1 ? '-' . $key : null) . '"'; $checked = in_array($key, $fieldValue) ? ' checked' : null; $html .= '<li><label><input type="checkbox" name="' . $fieldName . '" value="' . $key . '"' . $attrId . $checked . $required . '> ' . $val . '</label></li>'; } $html .= '</ul>'; return $html; }
/** * @param OptionsField $field * @return string * @throws \UnexpectedValueException */ public function render(OptionsField $field) { $required = $field->hasRule('ch\\metanet\\formHandler\\rule\\RequiredRule') === true ? ' aria-required="true"' : null; $html = '<ul' . $this->getAttributesAsHtml() . '>'; foreach ($field->getOptions() as $key => $val) { $label = $val; $htmlAttributes = ''; $htmlBefore = null; $htmlAfter = null; if (is_array($val) === true) { $label = $val['label']; $htmlAfter = isset($val['html_after']) ? $val['html_after'] : null; $htmlBefore = isset($val['html_before']) ? $val['html_before'] : null; if (isset($val['attributes']) === true) { foreach ($val['attributes'] as $attrName => $attrValue) { $htmlAttributes .= ' ' . $attrName . '="' . $attrValue . '"'; } } } $attrId = ' id="' . $field->getName() . '-' . $key . '"'; $checked = $key == $field->getValue() ? ' checked' : null; $html .= '<li>' . $htmlBefore . '<label><input type="radio" name="' . $field->getName() . '" value="' . $key . '"' . $attrId . $checked . $htmlAttributes . $required . '> ' . $label . '</label>' . $htmlAfter . '</li>'; } $html .= '</ul>'; return $html; }
/** * @param OptionsField $field * @return string */ public function render(OptionsField $field) { /** @var OptionsField $field */ $fieldValue = is_array($field->getValue()) ? $field->getValue() : array(); $html = '<select name="' . $field->getFormIdentifierAsString() . '[]" id="' . $field->getName() . '"' . $this->getAttributesAsHtml() . '>'; $html .= $this->renderOptions($field->getOptions(), $fieldValue); $html .= '</select>'; return $html; }
/** * @param OptionsField $field The OptionField to render * @return string The HTML of the rendered select field */ public function render(OptionsField $field) { $required = $field->hasRule('ch\\metanet\\formHandler\\rule\\RequiredRule') === true ? ' aria-required="true"' : null; $html = '<select name="' . $field->getFormIdentifierAsString() . '" id="' . $field->getName() . '"' . $this->getAttributesAsHtml() . $required . '>'; // Render the alternate options structure if set else fallback to the fields options array $html .= $this->renderOptions(is_array($this->renderOptions) ? $this->renderOptions : $field->getOptions(), $field->getValue()); $html .= '</select>'; return $html; }
/** * @param OptionsField $field The field instance to render * @return string The rendered field */ public function render(OptionsField $field) { $html = '<ul' . $this->getAttributesAsHtml() . '>'; $fieldName = $field->getName(); $fieldValue = $field->getValue(); $defaultAttributes = array('type' => 'radio', 'name' => $fieldName, 'value' => null); foreach ($field->getOptions() as $value => $data) { $defaultAttributes['value'] = $value; $attributesStr = ''; if (isset($data['attributes']) === true && is_array($data['attributes']) === true) { foreach (array_merge($defaultAttributes, $data['attributes']) as $name => $value) { $attributesStr .= ' ' . $name . '="' . (is_array($value) ? implode(' ', $value) : $value) . '"'; } } $selected = $value == $fieldValue ? ' checked' : null; $html .= '<li><label><input type="radio" name="' . $fieldName . '" value="' . $value . '"' . $attributesStr . $selected . '> ' . $data['label'] . '</label></li>'; } $html .= '</ul>'; return $html; }