--- /dev/null
+<select
+ id="{$field->getPrefixedId()}"
+ name="{$field->getPrefixedId()}"
+ {if !$field->getFieldClasses()|empty} class="{implode from=$field->getFieldClasses() item='class' glue=' '}{$class}{/implode}"{/if}
+ {if $field->isRequired()} required{/if}
+>
+ <option value="">{lang}wcf.global.noSelection{/lang}</option>
+ {foreach from=$field->getNestedOptions() item=__fieldNestedOption}
+ <option
+ value="{$__fieldNestedOption[value]}"
+ {if $field->getValue() == $__fieldNestedOption[value] && $__fieldNestedOption[isSelectable]} selected{/if}
+ {if $field->isImmutable() || !$__fieldNestedOption[isSelectable]} disabled{/if}
+ >{@' '|str_repeat:$__fieldNestedOption[depth] * 4}{@$__fieldNestedOption[label]}</option>
+ {/foreach}
+</select>
--- /dev/null
+<select
+ id="{$field->getPrefixedId()}"
+ name="{$field->getPrefixedId()}"
+ {if !$field->getFieldClasses()|empty} class="{implode from=$field->getFieldClasses() item='class' glue=' '}{$class}{/implode}"{/if}
+ {if $field->isRequired()} required{/if}
+>
+ <option value="">{lang}wcf.global.noSelection{/lang}</option>
+ {foreach from=$field->getNestedOptions() item=__fieldNestedOption}
+ <option
+ value="{$__fieldNestedOption[value]}"
+ {if $field->getValue() == $__fieldNestedOption[value] && $__fieldNestedOption[isSelectable]} selected{/if}
+ {if $field->isImmutable() || !$__fieldNestedOption[isSelectable]} disabled{/if}
+ >{@' '|str_repeat:$__fieldNestedOption[depth] * 4}{@$__fieldNestedOption[label]}</option>
+ {/foreach}
+</select>
--- /dev/null
+<?php
+
+namespace wcf\system\form\builder\field;
+
+use wcf\system\form\builder\field\validation\FormFieldValidationError;
+
+/**
+ * Implementation of a form field for selecting a single value.
+ *
+ * @author Matthias Schmidt, Marcel Werk
+ * @copyright 2001-2023 WoltLab GmbH
+ * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
+ * @since 6.0
+ */
+final class SelectFormField extends AbstractFormField implements
+ ICssClassFormField,
+ IImmutableFormField
+{
+ use TCssClassFormField;
+ use TImmutableFormField;
+ use TSelectionFormField;
+
+ /**
+ * @inheritDoc
+ */
+ protected $javaScriptDataHandlerModule = 'WoltLabSuite/Core/Form/Builder/Field/Value';
+
+ /**
+ * @inheritDoc
+ */
+ protected $templateName = '__selectFormField';
+
+ /**
+ * @inheritDoc
+ */
+ public function getSaveValue()
+ {
+ if ($this->getValue() === '') {
+ return;
+ }
+
+ return parent::getSaveValue();
+ }
+
+ /**
+ * @inheritDoc
+ */
+ public function readValue()
+ {
+ if ($this->getDocument()->hasRequestData($this->getPrefixedId())) {
+ $value = $this->getDocument()->getRequestData($this->getPrefixedId());
+
+ if (\is_string($value)) {
+ $this->value = $value;
+ }
+ }
+
+ return $this;
+ }
+
+ /**
+ * @inheritDoc
+ */
+ public function validate()
+ {
+ if ($this->getValue() === '') {
+ if ($this->isRequired()) {
+ $this->addValidationError(new FormFieldValidationError('empty'));
+ }
+ } else if (!isset($this->getOptions()[$this->getValue()])) {
+ $this->addValidationError(new FormFieldValidationError(
+ 'invalidValue',
+ 'wcf.global.form.error.noValidSelection'
+ ));
+ }
+
+ parent::validate();
+ }
+
+ /**
+ * @inheritDoc
+ */
+ public function value($value)
+ {
+ // ignore `null` as value which can be passed either for nullable
+ // fields or as value if no options are available
+ if ($value === null) {
+ return $this;
+ }
+
+ if (!isset($this->getOptions()[$value])) {
+ throw new \InvalidArgumentException("Unknown value '{$value}' for field '{$this->getId()}'.");
+ }
+
+ return parent::value($value);
+ }
+}