Skip to content

Commit

Permalink
fix single checkbox/radio generation
Browse files Browse the repository at this point in the history
  • Loading branch information
MacGyer committed Mar 24, 2018
1 parent 9b89dcc commit 44d75b5
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 4 deletions.
76 changes: 76 additions & 0 deletions src/lib/Html.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'] = '<span>' . $options['label'] . '</span>';
}

$checked = "$value" === "{$options['value']}";

if (!array_key_exists('id', $options)) {
$options['id'] = static::getInputId($model, $attribute);
}

return static::$type($name, $checked, $options);
}
}
33 changes: 29 additions & 4 deletions src/widgets/form/ActiveField.php
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ protected function initAutoComplete(&$options = [])
* ```
*
* @return string the rendering result
* @throws \Exception
*/
public function render($content = null)
{
Expand Down Expand Up @@ -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;
}

/**
Expand Down Expand Up @@ -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;
}

/**
Expand Down

0 comments on commit 44d75b5

Please sign in to comment.