We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

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.

public function beforeSave()
    {
        if (is_int($this->pub)) {
            $this->setPub(0);
        } else {
            $this->setPub(1);
        }

    }

and when getter\setter

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.



98.9k

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.

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

Some code:

// 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);
}
// NumberForm
public function initialize()
{
        $pub = new \Phalcon\Forms\Element\Check('pub');
        $pub->setLabel('Pub');

        $this->add($pub);
}
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.

// Mongo console
> db.number.find()
{ "_id" : 1, "pub" : "" }
{ "_id" : 2, "pub" : "0" }

To solve that problem I modified my Collection:

// 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:

// Mongo console
> db.number.find()
{ "_id" : 1, "pub" : 0 } // good staff
{ "_id" : 2, "pub" : 1 } // good staff