Search code examples
checkboxyii2

How to make Yii2's checkbox label not clickable?


I need to disable the possiblity to check the checkbox by clicking on the label. User should be able to check the checkbox only on clicking the box itself. I need to assign different action to click on label. How should I do that?

This is my code atm:

echo $form->field($model, 'accept')->checkbox(['template' => '{input}{beginLabel}{labelTitle}{endLabel}']);

That is something that supposed to work in Yii 1.x, but is not working in Yii2.


Solution

  • I am not sure , but you can try this : BY giving padding , or custom label .

    <!-- CHECKBOX BUTTON DEFAULT LABEL -->
    <?= $form->field($model, 'population')->checkbox(); ?>
    <!-- CHECKBOX BUTTON WITHOUT LABEL -->
    <?= $form->field($model, 'population')->checkbox(array('label'=>'')); ?>
    <!-- CHECKBOX BUTTON WITH CUSTOM LABEL -->
    <?= $form->field($model, 'population')  ->checkbox(array('label'=>''))
                                            ->label('Gender'); ?>
    <!-- CHECKBOX BUTTON WITH LABEL OPTIONS, DISABLED AND STYLE PROPERTIES -->
    <?= $form->field($model, 'population')->checkbox(array(
                                    'label'=>'',
                                    'labelOptions'=>array('style'=>'padding:5px;'),
                                    'disabled'=>true                                
                                    ))
                                    ->label('Gender'); ?>
    

    Id it is not working then , Define a HTML label manual

    <label>Gender</label>
    

    and

     <?= $form->field($model, 'population')->checkbox(array('label'=>'')); ?>