'How can i pass a custom css class in a generated checkbox List with Yii2 form field?

As per question's title, I am strugling to pass a custom css class on each generated checkbox list using Yii2 form field.

Here is my function:

 public function getItems()
    {
        $items = [];
        
        foreach (explode("\n", $this->options) as $option) {
            if (strpos($option, '=>') !== false) {
                list($key, $value) = explode('=>', $option);
                $items[trim($key)] = Yii::t($this->propertyField->language_category, trim($value));
            } else {
                $items[] = Yii::t($this->propertyField->language_category, trim($option));
            }
        }

        return $items;
    } 

As it is now i get this list:

<div class="form-group field-propertyfields">
<label class="form-label">Property Amenities</label>
<div id="propertyfields-7" class="multiselect-checkboxes">
<label><input type="checkbox" name="propertyFields[7][]" value="1">Elevator</label>
<label><input type="checkbox" name="propertyFields[7][]" value="2">Parking</label>
<label><input type="checkbox" name="propertyFields[7][]" value="3">Fireplace</label>
</div>  
</div>

What i want is to add a custom class on each label of input so it look like this:

<label class="my-custom-class"><input type="checkbox" name="propertyFields[7][]"value="2">Parking</label>

Here is how i render checkbox list:

public function getList($form, $model, $searchAttribute, $options = [])
    {

    $options['class'] = 'multiselect-checkboxes';        
    $formName = "{$searchAttribute}[{$this->propertyField->id}]";
   
    return $form->field($model, $formName)->checkboxList($this->getItems(), $options)->label($this->propertyField->getFieldTitle());
    }


Solution 1:[1]

$options['class'] = 'multiselect-checkboxes';

add one more array in your options variable - 'itemOptions'.

$options['itemOptions'] = ['class'=>'your class name for checkbox inputs'];

this will add your class on all checkbox inputs, further to add custom class on labels add 'labelOptions' to 'itemOptions'

$options['itemOptions']['labelOptions'] = ['class'=>'your class name for checkbox labels'];

Ref-https://www.yiiframework.com/doc/api/2.0/yii-widgets-activefield#checkboxList()-detail

further check the activeCheckboxList options

Solution 2:[2]

add it in your view file using jquery like this

$(".multiselect-checkboxes").children('label').addClass("my-custom-class");

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 JustCode