Skip to content

Commit 44d75b5

Browse files
committed
fix single checkbox/radio generation
1 parent 9b89dcc commit 44d75b5

File tree

2 files changed

+105
-4
lines changed

2 files changed

+105
-4
lines changed

src/lib/Html.php

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,4 +133,80 @@ private static function normalizeMaxLength($model, $attribute, &$options)
133133
}
134134
}
135135
}
136+
137+
/**
138+
* Generates a radio button tag together with a label for the given model attribute.
139+
* This method will generate the "checked" tag attribute according to the model attribute value.
140+
* @param Model $model the model object
141+
* @param string $attribute the attribute name or expression. See [[getAttributeName()]] for the format
142+
* about attribute expression.
143+
* @param array $options the tag options in terms of name-value pairs.
144+
* See [[booleanInput()]] for details about accepted attributes.
145+
*
146+
* @return string the generated radio button tag
147+
*/
148+
public static function activeRadio($model, $attribute, $options = [])
149+
{
150+
return static::activeBooleanInput('radio', $model, $attribute, $options);
151+
}
152+
153+
/**
154+
* Generates a checkbox tag together with a label for the given model attribute.
155+
* This method will generate the "checked" tag attribute according to the model attribute value.
156+
* @param Model $model the model object
157+
* @param string $attribute the attribute name or expression. See [[getAttributeName()]] for the format
158+
* about attribute expression.
159+
* @param array $options the tag options in terms of name-value pairs.
160+
* See [[booleanInput()]] for details about accepted attributes.
161+
*
162+
* @return string the generated checkbox tag
163+
*/
164+
public static function activeCheckbox($model, $attribute, $options = [])
165+
{
166+
return static::activeBooleanInput('checkbox', $model, $attribute, $options);
167+
}
168+
169+
/**
170+
* Generates a boolean input
171+
* This method is mainly called by [[activeCheckbox()]] and [[activeRadio()]].
172+
* @param string $type the input type. This can be either `radio` or `checkbox`.
173+
* @param Model $model the model object
174+
* @param string $attribute the attribute name or expression. See [[getAttributeName()]] for the format
175+
* about attribute expression.
176+
* @param array $options the tag options in terms of name-value pairs.
177+
* See [[booleanInput()]] for details about accepted attributes.
178+
* @return string the generated input element
179+
* @since 2.0.9
180+
*/
181+
protected static function activeBooleanInput($type, $model, $attribute, $options = [])
182+
{
183+
$name = isset($options['name']) ? $options['name'] : static::getInputName($model, $attribute);
184+
$value = static::getAttributeValue($model, $attribute);
185+
186+
if (!array_key_exists('value', $options)) {
187+
$options['value'] = '1';
188+
}
189+
if (!array_key_exists('uncheck', $options)) {
190+
$options['uncheck'] = '0';
191+
} elseif ($options['uncheck'] === false) {
192+
unset($options['uncheck']);
193+
}
194+
if (!array_key_exists('label', $options)) {
195+
$options['label'] = static::encode($model->getAttributeLabel(static::getAttributeName($attribute)));
196+
} elseif ($options['label'] === false) {
197+
unset($options['label']);
198+
}
199+
200+
if (isset($options['label'])) {
201+
$options['label'] = '<span>' . $options['label'] . '</span>';
202+
}
203+
204+
$checked = "$value" === "{$options['value']}";
205+
206+
if (!array_key_exists('id', $options)) {
207+
$options['id'] = static::getInputId($model, $attribute);
208+
}
209+
210+
return static::$type($name, $checked, $options);
211+
}
136212
}

src/widgets/form/ActiveField.php

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,7 @@ protected function initAutoComplete(&$options = [])
193193
* ```
194194
*
195195
* @return string the rendering result
196+
* @throws \Exception
196197
*/
197198
public function render($content = null)
198199
{
@@ -248,10 +249,22 @@ public function icon()
248249
* Materialize standard to not wrap the checkboxes in labels.
249250
* @return $this
250251
*/
251-
public function checkbox($options = [], $enclosedByLabel = false)
252+
public function checkbox($options = [], $enclosedByLabel = true)
252253
{
253254
Html::addCssClass($this->options, ['class' => 'checkbox']);
254-
return parent::checkbox($options, $enclosedByLabel);
255+
Html::removeCssClass($this->options, 'input-field');
256+
257+
$this->parts['{input}'] = Html::activeCheckbox($this->model, $this->attribute, $options);
258+
$this->parts['{label}'] = '';
259+
260+
if ($this->form->validationStateOn === ActiveForm::VALIDATION_STATE_ON_INPUT) {
261+
$this->addErrorClassIfNeeded($options);
262+
}
263+
264+
$this->addAriaAttributes($options);
265+
$this->adjustLabelFor($options);
266+
267+
return $this;
255268
}
256269

257270
/**
@@ -283,10 +296,22 @@ public function dropDownList($items, $options = [])
283296
* Materialize standard to not wrap the checkboxes in labels.
284297
* @return $this
285298
*/
286-
public function radio($options = [], $enclosedByLabel = false)
299+
public function radio($options = [], $enclosedByLabel = true)
287300
{
288301
Html::addCssClass($this->options, ['class' => 'radio']);
289-
return parent::radio($options, $enclosedByLabel);
302+
Html::removeCssClass($this->options, 'input-field');
303+
304+
$this->parts['{input}'] = Html::activeRadio($this->model, $this->attribute, $options);
305+
$this->parts['{label}'] = '';
306+
307+
if ($this->form->validationStateOn === ActiveForm::VALIDATION_STATE_ON_INPUT) {
308+
$this->addErrorClassIfNeeded($options);
309+
}
310+
311+
$this->addAriaAttributes($options);
312+
$this->adjustLabelFor($options);
313+
314+
return $this;
290315
}
291316

292317
/**

0 commit comments

Comments
 (0)