1

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());
    }
6
  • Is your checkbox list generated by you or from users? You don't need this kind of logic for normal forms. Commented Mar 18, 2022 at 13:18
  • its generated by me. The check boxes are a list that i use as filters in my search form. Commented Mar 18, 2022 at 13:18
  • then when you write label element write your custom class too. i don't see your use case with this logic if it's predefined form. Add code for how you render checkbox list from $items Commented Mar 18, 2022 at 13:19
  • Sorry. Misunderstood let me update my code plz. Commented Mar 18, 2022 at 13:27
  • 2
    Check the accepted answer in this question stackoverflow.com/questions/63939208/… checkboxList works in same way. Commented Mar 18, 2022 at 13:48

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

Comments

0

add it in your view file using jquery like this

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

3 Comments

Yep that's an option but I would prefer to go with php
...and why are people still assuming that everyone uses/want to use jQuery?
why don't you go for yii2 checkbox list? you can add your custom class and other attributes as well forum.yiiframework.com/t/how-to-work-with-checkboxlist/80792

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.