From 44d75b52d1c35bec2b4b22d158c4353dc35b5f9e Mon Sep 17 00:00:00 2001 From: Christoph Erdmann Date: Sat, 24 Mar 2018 14:36:21 +0100 Subject: [PATCH] fix single checkbox/radio generation --- src/lib/Html.php | 76 ++++++++++++++++++++++++++++++++ src/widgets/form/ActiveField.php | 33 ++++++++++++-- 2 files changed, 105 insertions(+), 4 deletions(-) diff --git a/src/lib/Html.php b/src/lib/Html.php index a01e1f3..0795062 100755 --- a/src/lib/Html.php +++ b/src/lib/Html.php @@ -133,4 +133,80 @@ private static function normalizeMaxLength($model, $attribute, &$options) } } } + + /** + * Generates a radio button tag together with a label for the given model attribute. + * This method will generate the "checked" tag attribute according to the model attribute value. + * @param Model $model the model object + * @param string $attribute the attribute name or expression. See [[getAttributeName()]] for the format + * about attribute expression. + * @param array $options the tag options in terms of name-value pairs. + * See [[booleanInput()]] for details about accepted attributes. + * + * @return string the generated radio button tag + */ + public static function activeRadio($model, $attribute, $options = []) + { + return static::activeBooleanInput('radio', $model, $attribute, $options); + } + + /** + * Generates a checkbox tag together with a label for the given model attribute. + * This method will generate the "checked" tag attribute according to the model attribute value. + * @param Model $model the model object + * @param string $attribute the attribute name or expression. See [[getAttributeName()]] for the format + * about attribute expression. + * @param array $options the tag options in terms of name-value pairs. + * See [[booleanInput()]] for details about accepted attributes. + * + * @return string the generated checkbox tag + */ + public static function activeCheckbox($model, $attribute, $options = []) + { + return static::activeBooleanInput('checkbox', $model, $attribute, $options); + } + + /** + * Generates a boolean input + * This method is mainly called by [[activeCheckbox()]] and [[activeRadio()]]. + * @param string $type the input type. This can be either `radio` or `checkbox`. + * @param Model $model the model object + * @param string $attribute the attribute name or expression. See [[getAttributeName()]] for the format + * about attribute expression. + * @param array $options the tag options in terms of name-value pairs. + * See [[booleanInput()]] for details about accepted attributes. + * @return string the generated input element + * @since 2.0.9 + */ + protected static function activeBooleanInput($type, $model, $attribute, $options = []) + { + $name = isset($options['name']) ? $options['name'] : static::getInputName($model, $attribute); + $value = static::getAttributeValue($model, $attribute); + + if (!array_key_exists('value', $options)) { + $options['value'] = '1'; + } + if (!array_key_exists('uncheck', $options)) { + $options['uncheck'] = '0'; + } elseif ($options['uncheck'] === false) { + unset($options['uncheck']); + } + if (!array_key_exists('label', $options)) { + $options['label'] = static::encode($model->getAttributeLabel(static::getAttributeName($attribute))); + } elseif ($options['label'] === false) { + unset($options['label']); + } + + if (isset($options['label'])) { + $options['label'] = '' . $options['label'] . ''; + } + + $checked = "$value" === "{$options['value']}"; + + if (!array_key_exists('id', $options)) { + $options['id'] = static::getInputId($model, $attribute); + } + + return static::$type($name, $checked, $options); + } } diff --git a/src/widgets/form/ActiveField.php b/src/widgets/form/ActiveField.php index 72fa484..27425b8 100755 --- a/src/widgets/form/ActiveField.php +++ b/src/widgets/form/ActiveField.php @@ -193,6 +193,7 @@ protected function initAutoComplete(&$options = []) * ``` * * @return string the rendering result + * @throws \Exception */ public function render($content = null) { @@ -248,10 +249,22 @@ public function icon() * Materialize standard to not wrap the checkboxes in labels. * @return $this */ - public function checkbox($options = [], $enclosedByLabel = false) + public function checkbox($options = [], $enclosedByLabel = true) { Html::addCssClass($this->options, ['class' => 'checkbox']); - return parent::checkbox($options, $enclosedByLabel); + Html::removeCssClass($this->options, 'input-field'); + + $this->parts['{input}'] = Html::activeCheckbox($this->model, $this->attribute, $options); + $this->parts['{label}'] = ''; + + if ($this->form->validationStateOn === ActiveForm::VALIDATION_STATE_ON_INPUT) { + $this->addErrorClassIfNeeded($options); + } + + $this->addAriaAttributes($options); + $this->adjustLabelFor($options); + + return $this; } /** @@ -283,10 +296,22 @@ public function dropDownList($items, $options = []) * Materialize standard to not wrap the checkboxes in labels. * @return $this */ - public function radio($options = [], $enclosedByLabel = false) + public function radio($options = [], $enclosedByLabel = true) { Html::addCssClass($this->options, ['class' => 'radio']); - return parent::radio($options, $enclosedByLabel); + Html::removeCssClass($this->options, 'input-field'); + + $this->parts['{input}'] = Html::activeRadio($this->model, $this->attribute, $options); + $this->parts['{label}'] = ''; + + if ($this->form->validationStateOn === ActiveForm::VALIDATION_STATE_ON_INPUT) { + $this->addErrorClassIfNeeded($options); + } + + $this->addAriaAttributes($options); + $this->adjustLabelFor($options); + + return $this; } /**