Add radio button form field
authorMatthias Schmidt <gravatronics@live.com>
Mon, 28 May 2018 18:40:34 +0000 (20:40 +0200)
committerMatthias Schmidt <gravatronics@live.com>
Mon, 28 May 2018 18:40:34 +0000 (20:40 +0200)
See #2509

wcfsetup/install/files/acp/templates/__radioButtonFormField.tpl [new file with mode: 0644]
wcfsetup/install/files/lib/system/form/builder/field/RadioButtonFormField.class.php [new file with mode: 0644]
wcfsetup/install/files/lib/system/form/builder/field/TSelectionFormField.class.php

diff --git a/wcfsetup/install/files/acp/templates/__radioButtonFormField.tpl b/wcfsetup/install/files/acp/templates/__radioButtonFormField.tpl
new file mode 100644 (file)
index 0000000..0a8b701
--- /dev/null
@@ -0,0 +1,7 @@
+{include file='__formFieldHeader'}
+
+{foreach from=$field->getOptions() key=$__fieldValue item=__fieldLabel}
+       <label><input type="radio" name="{@$field->getPrefixedId()}" value="{$__fieldValue}"{if $field->getValue() === $__fieldValue} checked{/if}> {@$__fieldLabel}</label>
+{/foreach}
+
+{include file='__formFieldFooter'}
diff --git a/wcfsetup/install/files/lib/system/form/builder/field/RadioButtonFormField.class.php b/wcfsetup/install/files/lib/system/form/builder/field/RadioButtonFormField.class.php
new file mode 100644 (file)
index 0000000..0353cb4
--- /dev/null
@@ -0,0 +1,27 @@
+<?php
+namespace wcf\system\form\builder\field;
+
+/**
+ * Implementation of a radio buttons form field for selecting a single value.
+ * 
+ * @author     Matthias Schmidt
+ * @copyright  2001-2018 WoltLab GmbH
+ * @license    GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
+ * @package    WoltLabSuite\Core\System\Form\Builder\Field
+ * @since      3.2
+ */
+class RadioButtonFormField extends AbstractFormField implements ISelectionFormField {
+       use TSelectionFormField;
+       
+       /**
+        * @inheritDoc
+        */
+       protected $templateName = '__radioButtonFormField';
+       
+       /**
+        * @inheritDoc
+        */
+       protected static function supportsNestedOptions(): bool {
+               return false;
+       }
+}
index fdb25a800c505395f347f011a0b795ef6bb858b7..259184c74625a4d565392b927e4aad249e2adea4 100644 (file)
@@ -100,7 +100,12 @@ trait TSelectionFormField {
                $validateOptions = function(array &$array) use (&$validateOptions) {
                        foreach ($array as $key => $value) {
                                if (is_array($value)) {
-                                       $validateOptions($value);
+                                       if (static::supportsNestedOptions()) {
+                                               $validateOptions($value);
+                                       }
+                                       else {
+                                               throw new \InvalidArgumentException("Option '{$key}' must not be an array.");
+                                       }
                                }
                                else {
                                        if (!is_string($value) && !is_numeric($value)) {
@@ -166,4 +171,13 @@ trait TSelectionFormField {
                
                return parent::value($value);
        }
+       
+       /**
+        * Returns `true` if the field class supports nested options and `false` otherwise.
+        * 
+        * @return      bool
+        */
+       protected static function supportsNestedOptions(): bool {
+               return true;
+       }
 }