options[$value])) throw new InputException("Could not add value to field. Value already exists."); // Add to the values if (is_null($label)) $this->options[$value] = $value; else $this->options[$value] = $label; return $this; } /** * Bulk add options to the RadioField * * @param array $options Associative array [name => label, name => label] * @return $this * @throws InputException */ public function addOptions(array $options): self { foreach ($options as $key => $val) $this->addOption($key, $val); return $this; } /** * Pre-check one of the options * * @param string $optionName Name of the option to check * @return $this */ public function check(string $optionName): self { $this->setValue($optionName); return $this; } public function getOptions(): array { return $this->options; } protected function validateField(): bool { if (isset($this->options[$this->value])) { $this->valid = true; return $this->valid; } $this->valid = false; $this->errors[] = $this->getLabel() . " was provided an illegal option."; return $this->valid; } public function generateHtml(): string { $name = "name='".$this->getName()."'"; $lock = $this->lock ? "disabled" : ""; $class = "class='".implode(" ", $this->classNames)."'"; $out = "
"; foreach ($this->options as $key => $val) { $uni_id = $this->getId() . "_" . $key; $id = "id='" . $uni_id . "'"; $value = "value='".$key."'"; $checked = $key === $this->getValue() ? "checked" : ""; $out .= "
"; $out .= ""; $out .= ""; $out .= "
"; } $out .= "
"; return $out; } }