No options needed on checkbox yii2, Only label at the top is required

384 views Asked by At

yii2 checkbox value is not required. Only the label is required at the top in yii2 active form.

2

There are 2 answers

2
soodssr On

You can use simple tag() method of HTML helper class in Yii 2. As follows:

Html::tag('label', 'LABEL_TEXT');

Or you can mention only {label} for the template option for that particular field like:

$form->field($model, ATTRIBUTE_NAME, ['template' => '{label}'])->checkbox();
0
user206 On

There are many ways. You probably want to create a Toggle Switch or ... .

<?= $form->field($model, 'Attribute')->checkbox([
  'label' => false, // <label> is the parent of <input>
  'style' => 'visibility: hidden !important;', // Or display: none;
])->label('yourLabel', ['style' => 'display:block',]); ?>

Also if the user clicks on the text within the <label> element, it toggles the control. You can use yii\bootstrap\ActiveForm instead of yii\widgets\ActiveForm for more options.