/** * @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 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 * @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; }