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

Multiple Checkbox render

Hello,

I was wondering how to create a multiple checkbox for a form in Phalcon ? The goal is to produce :

<input type="checkbox" name="name[]" value="1"/>

<input type="checkbox" name="name[]" value="2"/>

This two fields must be required. So, if the user doesn't check the two options, i would like that isValid function checks the already checked one.

Can anyone help me please ?

I'm using radio/checkbox by this way:

class ExampleForm extends Form {
        …
        $group = new Check('group', array(
            'name' => 'group[]',
        ));
        $this->add($group);
        …
}

And template:

<label class="radio-inline radio-primary">
    {{ form.render('group', {'value':'10'}) }}
    {{ _('Customer') }}
</label>
<label class="radio-inline radio-primary">
    {{ form.render('group', {'value':'01'}) }}
    {{ _('Supplier') }}
</label>


5.1k

And could you tell me how do you check values after a $_POST but on isValid = false. Like i said before, if my two fields are required, and if i check only one, after post i want to display form error, but keeping the checked box ?

edited May '14

Ok, you can do it this way:

class ExampleForm extends Form {
        …
        $group1 = new Check('group1', array(
            'name' => 'group[]',
            'value' => '1'
        ));
        $group1->addValidator(new PresenceOf(array(
            'message' => 'Group 1 is required'
        )));
        $this->add($group1);

        $group2 = new Check('group2', array(
            'name' => 'group[]',
            'value' => '2'
        ));
        $group2->addValidator(new PresenceOf(array(
            'message' => 'Group 2 is required'
        )));
        $this->add($group2);
        …
}

Template:

<label class="radio-inline radio-primary">
    {{ form.render('group1') }}
    {{ _('Customer') }}
</label>
<label class="radio-inline radio-primary">
    {{ form.render('group2') }}
    {{ _('Supplier') }}
</label>


5.1k

Ok, but i guess we misunderstood on the validation part. Currently, if i use your solution, this is what i get :

But as you can see, even if the two fields are required, field one after post is not checked. Actually i would like this result (i've edit the html code to produce the screen) :

Thanks,

Okey, I check this on real form and I see that the problem is name of Checkbox (<input name="group[]">). When I remove name parameter validation works and form remeber state:

        $group1 = new Check('group1', array(
            //'name' => 'group[]',
            'value' => '1'
        ));
        $group1->addValidator(new PresenceOf(array(
            'message' => 'Group 1 is required'
        )));
        $this->add($group1);

        $group2 = new Check('group2', array(
            //'name' => 'group[]',
            'value' => '2'
        ));
        $group2->addValidator(new PresenceOf(array(
            'message' => 'Group 2 is required'
        )));
        $this->add($group2);

I think that is to do. Maybe custom validator is needed.



5.1k

I understand the solution, but this is not the right behavior... I would like an array as a name to loop after isValid()... But maybe there's no solution for the moment on Phalcon. Someone of the dev team can maybe bring more informations ? Or example ?

Anyway thanks Marcin for your support.



17.7k
edited Mar '15

try this

$values = array(1,2,3,4,5,6);
$group; 
for($i=0; $i<count($values);$i++){
        $group = new Check("group".$i,array(
            "name" => "group[]",
            "value" => $values[$i]
        ));
        $this->add($group);
    }

Have you tried using session variables such that in the checkbox the value whether checked or unchecked are rendered through initialized boolean variables which can be re-assigned based on the conditions you want met.

This can also be accomplished by using initalize function in your controller which can be changed inside your action based on whether the condition you want is met or not.