public function getRadioCheckbox($renderApi, $unit, $fieldId, $postRequestValue = null, $required = false) { $formField = new \Fieldset(); $listOptions = $this->listOptions->getListOptions($renderApi, $unit); $inputName = strlen($renderApi->getFormValue($unit, 'inputName')) > 0 ? $renderApi->getFormValue($unit, 'inputName') . "[]" : $fieldId . "[]"; if ($listOptions->hasOptions()) { $optionCount = 0; $options = $listOptions->getOptions(); $optionsLength = count($options); foreach ($options as $option) { /* @var $option \Option */ $properties = $unit->getFormValues(); if ($properties["listType"] === \ListType::RADIO) { $choiceField = new \RadioButtonField(); } elseif ($properties["listType"] === \ListType::CHECKBOX) { $choiceField = new \CheckboxField(); } $optionId = $fieldId . '_' . $optionCount; $elementProperties = $choiceField->getElementProperties(); $elementProperties->addAttribute("value", $option->getValue()); $elementProperties->addAttribute("name", $inputName); $elementProperties->addAttribute("id", $optionId); // set required attribute for radio options or when there is only one checkbox // don't set for multiple checkboxes to match server-side validation logic if ($required && ($properties["listType"] === \ListType::RADIO || $optionsLength === 1)) { $elementProperties->addAttribute("required", null); } $request = new Request(); $request->isPostRequest(); if (!$request->isPostRequest() && $option->isChecked() || !is_null($postRequestValue) && in_array($option->getValue(), $postRequestValue)) { $elementProperties->addAttribute("checked", null); } $label = new \Label(); $label->add($choiceField); $label->add(new \Span($option->getName())); $labelProperties = $label->getElementProperties(); $labelProperties->addAttribute("for", $optionId); $formField->add($label); $optionCount++; } } if ($this->formSubmit->isValid($renderApi, $unit) && !$this->isValidValue($unit, $postRequestValue)) { $formField->add($this->getErrorMessage($unit, $postRequestValue)); $formField->getElementProperties()->addClass('vf__error'); } return $formField; }