Checkbox checked value does not set by Model's Load function

Hi all!

I have a big problem. I created a checkbox with the ActiveForm, when I checked this checkbox then the posted value will be true, but when the model’s load function is called, the load function will not set the checkbox’s field value… for me it seems the load function does not handle ActiveForm’s checkbox value.

View:




<?php if ($model->avatar): ?>


   <?= $form->field($model, 'avatarToDelete')->checkbox(['label' => 'Do you want to delete avatar?']) ?>


<?php endif; ?>



The model’s part:




/**

 * Profile form

 */

class ProfileForm extends Model {


    public $lastName;

    public $firstName;

    public $birthday;

    public $avatar;

    public $avatarUrl;

    public $avatarToDelete;


    function __construct() {

        parent::__construct();

        if (Yii::$app->user->identity->profile) {

            $this->lastName = Yii::$app->user->identity->profile->last_name;

            $this->firstName = Yii::$app->user->identity->profile->first_name;

            $this->birthday = Yii::$app->user->identity->profile->birthday;

            $this->avatar = Yii::$app->user->identity->profile->avatar;

            $this->avatarUrl = Yii::$app->user->identity->profile->getThumbnailProfilePictureUrl();

            $this->avatarToDelete = false;

        }

    }

//other code...

}



The action in controller see commits:




   /**

     * Modfiy user's profile.

     *

     * @return mixed

     */

    public function actionUpdate()

    {

        $model = new ProfileForm();

        // This will ok. It has the checked value (=true) of avatarToDelete checkbox

        var_dump(Yii::$app->request->post());

        $model->load(Yii::$app->request->post());

        // But after load, I see the $avatarToDelete still equals with false...

        die(var_dump($model));

        if ($model->load(Yii::$app->request->post()) && $model->save()) {

            Yii::info(sprintf('Profile modified by user: %s.', Yii::$app->user->identity->username));

        }

        return $this->render('update', ['model' => $model]);

    }



If I change other field values on the form, then the load method will handle them correctly, except the checkbox’s value. I do not know why not… the avatarToDelete always will be false… but in the post I see the value is true when I check the checkbox on the view.

It seems there are not any bind/connections between the checkbox’s value and object field’s value.

Here the var_dumps value (I have changed the firstName and lastName value’s too on the form and the avatarToDelete, but as you see, the avatarToDelet’s value does not changed.):




// The post's var_dumps:

array(3) { ["_csrf"]=> string(56) "RXcwb3RBREUjKFYhNXRpLD0RARgTDHANNwADAzs0LgQmPFEYRnVyJw==" ["ProfileForm"]=> array(5) { ["lastName"]=> string(6) "YYYYYY" ["firstName"]=> string(5) "XXXX1" ["birthday"]=> string(10) "1986-06-18" ["avatar"]=> string(0) "" ["avatarToDelete"]=> string(1) "1" } ["signup-button"]=> string(0) "" } object(app\models\forms\ProfileForm)

// The model's var_dumps after load:

#48 (11) { ["lastName"]=> string(6) "YYYYYY" ["firstName"]=> string(5) "XXXX1" ["birthday"]=> string(10) "1986-06-18" ["avatar"]=> string(0) "" ["avatarUrl"]=> string(65) "public/uploads/user/avatar/thumbnail/avatar_56e726eb13b2c_yii.png" ["avatarToDelete"]=> int(0) ["_errors":"yii\base\Model":private]=> NULL 



Do you know where is the problem?

Do you have a rule for "avatarToDelete" in your model?

Yes, I forget to add a rule in the model. Thanks for the help :)