yii2 checkbox and hidden input value 0 when I update my record.
<input type="hidden" value="0" name="Model[active]">
<?= $form->field($model, ‘active’)->checkBox([‘label’ => ‘’,‘data-size’=>‘small’, ‘class’=>‘bs_switch’
,'style'=>'margin-bottom:4px;', 'id'=>'active']) ?>
why not this hidden filed value set from db, where active is 1.
Value is 0 because you are writing it in "value" attribute of hidden field.
I’m not writing it 0,it is by default 0. How I can change it value?
You should put value of hidden field from model:
<?php $model->active = 0; ?>
<?= $form->field($model, 'active')->checkBox(['label' => '','data-size'=>'small', 'class'=>'bs_switch'
,'style'=>'margin-bottom:4px;', 'id'=>'active']) ?>
bobonov
(Bobonov)
January 19, 2015, 10:38am
5
Your problem is not very clear but I try to guess…
As Fabrizio stated you should set the value for the hidden filed from the model.
But I see another problem:
<input type="hidden" value="0" name="Model[active]">
<?= $form->field($model, 'active')->checkBox(['label' => '','data-size'=>'small', 'class'=>'bs_switch'
,'style'=>'margin-bottom:4px;', 'id'=>'active']) ?>
This code generate 2 form field with the same name (also if you generate the hidden filed form the model), when you submit the value of the last one will overwrite the first in $_POST.
If you want to keep the original value you should use a different name for the hidden one or better use ActiverRecord during save to check original value.
If you explain the reason you need this maybe we can help you to find the optimal solution.
Your problem is not very clear but I try to guess…
As Fabrizio stated you should set the value for the hidden filed from the model.
But I see another problem:
<input type="hidden" value="0" name="Model[active]">
<?= $form->field($model, 'active')->checkBox(['label' => '','data-size'=>'small', 'class'=>'bs_switch'
,'style'=>'margin-bottom:4px;', 'id'=>'active']) ?>
This code generate 2 form field with the same name (also if you generate the hidden filed form the model), when you submit the value of the last one will overwrite the first in $_POST.
If you want to keep the original value you should use a different name for the hidden one or better use ActiverRecord during save to check original value.
If you explain the reason you need this maybe we can help you to find the optimal solution.
the hidden field automatic generates…
softark
(Softark)
July 18, 2016, 1:52pm
7
The hidden input is automatically generated by default.
See "uncheck" option in the API documentation.
http://www.yiiframework.com/doc-2.0/yii-widgets-activefield.html#checkbox()-detail
uncheck: string, the value associated with the uncheck state of the radio button. If not set, it will take the default value ‘0’. This method will render a hidden input so that if the radio button is not checked and is submitted, the value of this attribute will still be submitted to the server via the hidden input. If you do not want any hidden input, you should explicitly set this option as null.
(The doc says “radio”, but it should be “checkbox”. )
By this trick, Yii ensures that the form will always submit the value of the boolean attribute, whether it may be true or false.
Without the hidden input, the submitted post data will not contain the boolean field if the checkbox is not checked.
softark:
The hidden input is automatically generated by default.
See "uncheck" option in the API documentation.
http://www.yiiframework.com/doc-2.0/yii-widgets-activefield.html#checkbox()-detail
(The doc says “radio”, but it should be “checkbox”. )
By this trick, Yii ensures that the form will always submit the value of the boolean attribute, whether it may be true or false.
Without the hidden input, the submitted post data will not contain the boolean field if the checkbox is not checked.
yeah. it is a good solution for this problem