yii2-bootstrap4 icon indicating copy to clipboard operation
yii2-bootstrap4 copied to clipboard

ActiveField::checkboxList() does not show validation errors when $options['item'] is specified

Open ashaduri opened this issue 3 years ago • 0 comments

What steps will reproduce the problem?

Set up a validation rule:

class MyModel extends ActiveRecord {
	public function rules(): array
	{
		return [
			[['features'], 'required'],
		];
	}
	// ...
}

Create a form with a checkboxList:

use yii\bootstrap4\Html;
use yii\bootstrap4\ActiveForm;

$form = ActiveForm::begin([]);

$items = [
	'1' => "Option 1",
	'2' => "Option 2",
	'3' => "Option 3",
];

$checkbox_options = [
	'class' => 'form-control',
	'item' => function ($index, $label, $name, $checked, $value) {
		$options = [
			'label' => Html::encode($label),
			'value' => $value,
		];
		if ($value === '1') {
			$options['labelOptions'] = ['style' => "text-decoration: line-through;"];
		}
		return '<div class="checkbox">' . Html::checkbox($name, $checked, $options) . '</div>';
	},
];

print $form->field($model, 'features')->checkboxList($items, $checkbox_options)
	->hint("Enabled features.");

What's expected?

It is expected that if no checkboxes are checked, an error is displayed (ajax and / or server-side).

What do you get instead?

There is no error displayed. The form can be submitted, a validation error occurs, but it is not displayed under the checkbox list.

Possible cause

The whole \yii\bootstrap4\ActiveField::checkboxList() function behaves very differently if $options['item'] is not specified, and falls back to \yii\widgets\ActiveField's implementation.

Regression

Using yii\bootstrap\ActiveForm instead of bootstrap4 fixes this particular problem (the error is displayed), but the styles are (predictably) off.

Additional info

Q A
yiisoft/yii2-bootstrap4 2.0.10
Yii vesion 2.0.43
PHP version 7.4.6
Operating system Linux

ashaduri avatar Dec 15 '21 13:12 ashaduri