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

Phalcon\Forms\Element\Check works correctly???

Hello!!! Create a form with two element (Text, Check). After submitting the form data, the check mark is not saved. With text elements are no problems, they retain the entered values



98.9k

We're aware of that, and that is happening because (contrary to the expected), Phalcon does not compare the value in the check with the one that comes up from $_POST, instead of that the value is ignored because of the one already defined in the check's parameters, hope to change this behavior in a further release



98.9k

This implementation is now fixed in 1.2.0

Is it fixed in 1.2 beta1 or only in HEAD? I still encounter the problem with 1.2 beta1.



98.9k

Yes, it's fixed, check in the online Vokuro: https://vokuro.phalcon.io/session/signup, if you accept/don't accept the terms and conditions the check value is kept between reloads

Hey @Phalcon,

that's good to hear, because it means I'm doing something wrong :).

In my form I have a checkbox like this:

        $published = new Check('published', array(
            'value' => '1'
        ));
        $published->setLabel('Published?');
        $this->add($published);

I initialize this form like every other:

    public function initialize($entity)
    {

        $this->setEntity($entity);

When I create this form by passing an entity to it, for example $form = new MyForm(Entity::findFirst(1)); I get a form with the data from this entity pre-filled (title, text etc.), however my checkbox is never checked, no matter what value (1, '1', true) I make the entity pass along for it. Any ideas?



98.9k

ok, let me check what could be the problem there

While debugging for myself, I noticed that the value I defined above never makes it to the HTML source:

<input type="checkbox" value="" name="published" id="published" checked="checked">

I'm using the latest 1.2 code, by the way. Any ideas? :)

(I helped myself with select's for now, but either I'm doing something terribly wrong or the implementation is bugged :))



98.9k

@Philipp can you try again with the latest 1.2.0?

Same problem. 1.2.0 beta 2 - not works

Try to recompile again, 1.2.0 beta 3 is 3-4 days old

I can confirm that checkboxes now work with the latest HEAD. Thanks!

Thanks. It works. But I I was faced with a new problem. Details: https://gist.github.com/webtor/5968873

When checkbox is not checked, in $_POST this var isn't set. When I try save Entity, method set variable not called.

That would be the regular and expected behavior of any checkbox.

Try this out to confirm:

<?php var_dump($_POST); ?>
<form action="?" method="post">
<input type="checkbox" name="test" value="1" /> Check me out, I send "1" if checked and nothing if not checked.
<input type="submit" />
</form>

Use option fields to avoid that problem.

When we using Form with Entity

$form->bind($_POST, $entity);

Good framework must consider all elements in Form and all options in Entity. We don't must working with crutches and replace checkbox to options. According to this principle works ZendFramework, Symfony and others. I want that Phalcon was the best framework with useful and comfortable capabilities.

edited Oct '14

Is there any update on this? Currently binding entity to form with unmarked checkboxes won't set corresponding fields to anything, rather then '0'.