Checkbox default value is not being sent to the server

Here is my code :


     $model->free=true;

                echo $form->field($model, 'free',['inputOptions'=>['class'=>'form-control input-lg ']])->checkbox();



What I want is if the user doesn’t uncheck, the default value(1 or true) must be sent to the server but to my suprise it is sending $model->free is remaining null weither the user uncheck or leave it as it is; to send the checked value the user has to uncheck and check which stress.

How can I acheive what I want?

Show us your validator for ‘free’ field, please.

I think if you create your validation like


['free', 'boolean']

it should work correctly

Something like:


$adopted = (isset($_POST['adopted']) == '1' ? '1' : '0');

I use tinyint and store a 0 or 1.

That way in the create array:




$postdata = array(

                    'catpic' => $catpic,

                    'catname' => $catname,

                    'sex' => $sex,

                    'comments' => $comments,

                    'adopted' => $adopted,

                    'lastedit' => $lastedit

                );



Just change $_POST to a yii2 request.