'form-check-input',
'label' => $encode ? static::encode($label) : $label,
'labelOptions' => ['class' => 'form-check-label'],
'value' => $value,
], $itemOptions);
return '
' . static::radio($name, $checked, $options) . '
';
};
}
return parent::radioList($name, $selection, $items, $options);
}
/**
* {@inheritdoc}
*/
public static function checkboxList($name, $selection = null, $items = [], $options = []): string
{
if (!isset($options['item'])) {
$itemOptions = ArrayHelper::remove($options, 'itemOptions', []);
$encode = ArrayHelper::getValue($options, 'encode', true);
$options['item'] = function ($index, $label, $name, $checked, $value) use ($itemOptions, $encode) {
unset($index);
$options = array_merge(
[
'class' => 'form-check-input',
'label' => $encode ? static::encode($label) : $label,
'labelOptions' => ['class' => 'form-check-label'],
'value' => $value,
], $itemOptions);
return '' . Html::checkbox($name, $checked, $options) . '
';
};
}
return parent::checkboxList($name, $selection, $items, $options);
}
/**
* {@inheritdoc}
*/
public static function error($model, $attribute, $options = []): string
{
if (!array_key_exists('class', $options)) {
$options['class'] = ['invalid-feedback'];
}
return parent::error($model, $attribute, $options);
}
/**
* @inheritdoc
*/
protected static function booleanInput($type, $name, $checked = false, $options = []): string
{
$options['checked'] = (bool)$checked;
$value = array_key_exists('value', $options) ? $options['value'] : '1';
if (isset($options['uncheck'])) {
// add a hidden field so that if the checkbox is not selected, it still submits a value
$hiddenOptions = [];
if (isset($options['form'])) {
$hiddenOptions['form'] = $options['form'];
}
$hidden = static::hiddenInput($name, $options['uncheck'], $hiddenOptions);
unset($options['uncheck']);
} else {
$hidden = '';
}
if (isset($options['label'])) {
$label = $options['label'];
$labelOptions = $options['labelOptions'] ?? [];
unset($options['label'], $options['labelOptions']);
if (!isset($options['id'])) {
$options['id'] = static::getId();
}
$input = static::input($type, $name, $value, $options);
if (isset($labelOptions['wrapInput']) && $labelOptions['wrapInput']) {
unset($labelOptions['wrapInput']);
$content = static::label($input . $label, $options['id'], $labelOptions);
} else {
$content = $input . "\n" . static::label($label, $options['id'], $labelOptions);
}
return $hidden . $content;
}
return $hidden . static::input($type, $name, $value, $options);
}
/**
* Returns an autogenerated ID
* @return string Autogenerated ID
*/
protected static function getId(): string
{
return static::$autoIdPrefix . static::$counter++;
}
}