phphtmlyii2active-form

Checkbox ActiveForm Yii2


i want to change default value of checkbox. so when checked the value that will be stored in the database = 0, whereas if unchecked the data stored in the database = 1

I've seen some examples like

<?= $form->field($model, 'agent_email_verification')->checkbox(['checked' => $model->agent_email_verification > 0,  'value' => true]) ?>

and

<?php $model->status = 1; ?>
<?= $form->field($model, 'status')->checkbox()->label('Hi'); ?>

but when I save it to the database the values ​​I get are still checked = 1, uncheck = 0

Can you help me

I expect if i save data i get value checked = 0, uncheck = 1


Solution

  • For this purpose there's the uncheck option. Try

    <?= $form->field($model, 'status')->checkbox(['value' => 0, 'uncheck' => '1']); ?>

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