htmlcheckboxyii2checkboxlist

Yii2 ActiveForm CheckboxList Group Seperator


I am trying to place a line in between a list of values in a Yii2 CheckboxList. For now I am just trying to get it to work using the yii Bootstrap 5 Html form field.

When using this:

      <?= Html::checkboxList('field_name','',[
            'organisation_name'=>'Organisation Name',
            'organisation_vat_number'=>'Organisation VAT Number',
            'invoice_number'=>'Invoice Number',
            'invoice_date'=>'InvoiceDate,], 
     $options=['separator' => '<hr>', 'class'=>'form-control']) ?>

I get this:  

Organisation Name   
---------------------------
Organisation VAT Number
---------------------------
Invoice Number
---------------------------
Invoice Date
---------------------------



What I want is this:

Organisation Name   
Organisation VAT Number
---------------------------
Invoice Number
Invoice Date
---------------------------

Is this possible with HTML Bootstrap and how to implement with Yii2 ?


Solution

  • What I am thinking is to create a custom item rendered using the $index.

    Edited: Another workaround is to use the $key as a flag for your "separator".

    <?= Html::checkboxList('field_name', '', [
        'organisation_name'       => 'Organisation Name',
        'organisation_vat_number' => 'Organisation VAT Number',
        
        'separator'              => '',
    
        'invoice_number'          => 'Invoice Number',
        'invoice_date'            => 'Invoice Date',
        'another_menu'            => 'Another Menu',
        'another_menu'            => 'Another Menu',
        'another_menu'            => 'Another Menu',
        
        'separator'              => '',
    ], [
        'item' => function ($index, $label, $name, $checked, $value) {
            if ($value === 'separator') {
                return '<hr>';
            }
    
            return "<label class='form-control'>" . Html::checkbox($name, $checked, ['value' => $value]) . " $label</label>";
        }
    ]) ?>
    
    

    Our homework is to put the 'separator' key anywhere inside the array of $items.

    Result:

    HTML checkbox