Search code examples
yii2yii2-extension

Setting kartik switch input true and false value


Am using yii2 kartik switch input and I would like to set the switch input true and false value explicitly

This is the switch input

<?php

echo $form->field($model, 'PR_Status_ID')->widget(SwitchInput::classname(), [])->label(false);;
    ?>

Am using the switch input for updating a field and i would like the switch input to be off if the value of $model PR_Status_ID is 6 and on if the value is 7

How can I implement this?


Solution

  • Just set the initial value based on the PR_Status_ID field for update case:

        echo $form->field($model, 'PR_Status_ID')->widget(SwitchInput::classname(), [
             'value' => (!$model->isNewRecord && $model->PR_Status_ID == 6) ? false : true,
        ])->label(false);