Checkbox problem

I have a problem with checkbox. When his checked, value is (string) "", and when not checked value (int) 0. This is a bug? And for solve this, I must use beforeSave() method. ```php public function beforeSave() { if (is_int($this->pub)) { $this->setPub(0); } else { $this->setPub(1); }

}

``` and when getter\setter ```php public function getPub() { return (string) $this->pub;

}

public function setPub($pub)
{
    $this->pub = $pub;

}

```

what framework version you use? I had similar problems with 1.1.x version and it was fixed in 1.2 version.



85.0k

It compares values using the == operator which have that behavior, if we use the identical operator it would make the work harder because you have to pass the exact type to make the checkbox checked.

http://php.net/manual/en/language.operators.comparison.php

Some code: ```php // Controller public function editAction($id) { $id = (int) $id; $number = Number::findById($id);

    $form = new NumberForm($number);

    if ($this->request->isPost()) {
        $form->bind($_POST, $number);

        if ($form->isValid()) {
            $number->save();
            $this->uploadImage($id);
            $this->redirect('/newspaper/number/edit/' . $id);
        }
    } else {
        $form->setEntity($number);
    }

    $this->view->setVar('form', $form);
    $this->view->setVar('number', $number);

} ``` ```php // NumberForm public function initialize() { $pub = new \Phalcon\Forms\Element\Check('pub'); $pub->setLabel('Pub');

    $this->add($pub);

} ``` ```php class Number extends \Phalcon\Mvc\Collection // also tested with \Phalcon\Mvc\Model ... same trouble {

public $pub = 0;

public function getPub()
{
    return $this->pub;

}

public function setPub($pub)
{
    $this->pub = $pub;

}

} ``` At version 1.1 was problem with checkbox ... it was solved. But, not to the end. What problem? When I create a record with form, checkbox is checked, I get in my DB the "pub" value of this record = "" (string value). When I create a record and checkbox is not checked, I get in my DB the "pub" value of this record = 0 (integer value). Then ... I try edit my records, I call editAction ... and I recieve a CHECKED checkbox at 1 and 2 records. I try resave records with different checkbox values ... but theirs values has not been changed. ```javascript // Mongo console > db.number.find() { "id" : 1, "pub" : "" } { "id" : 2, "pub" : "0" } ```

To solve that problem I modified my Collection: ```php // Number Collection. Added beforeSave() method and modified getPub() public function beforeSave() { if (is_int($this->pub)) { $this->setPub(0); } else { $this->setPub(1); }

}

public function getPub() { return (string) $this->pub;

}

public function setPub($pub) { $this->pub = $pub;

} ``` Then all begins work correctly. Records in DB: ```javascript // Mongo console > db.number.find() { "id" : 1, "pub" : 0 } // good staff { "id" : 2, "pub" : 1 } // good staff ```