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

All radioButtons checked while one is supposed to only

Im definiing radio group:

<?php
    $a = new \Phalcon\Forms\Element\Radio('prepaytype_a', array('name' => 'prepaytype'));
    $a->setDefault(1);
    $a->setAttribute('checked', 'checked');
    $this->add($a);

    $b = new \Phalcon\Forms\Element\Radio('prepaytype_b', array('name' => 'prepaytype'));
    $b->setDefault(2);
    $this->add($b);

but they gets rendered like that:

<input type="radio" checked="checked" value="1" name="prepaytype" id="prepaytype_a">

<input type="radio" checked="checked" value="2" name="prepaytype" id="prepaytype_b">

while only the first should be checked.



85.5k

why is that ?

$b->setDefault(2);


28.2k

value for both radiobuttons. If I select A, I want to get 1, if I select B, I want to get 2.

How do you render the objects?



85.5k
Accepted
answer

i would do it:


$a = new \Phalcon\Forms\Element\Radio('prepaytype_a', array('name' => 'prepaytype', 'value' => 1));
$b = new \Phalcon\Forms\Element\Radio('prepaytype_b', array('name' => 'prepaytype', 'value' => 2));

//and if you still need to go back to this form and you want to be same as user left it

if ($_POST['prepaytype'] == 1){
    $a->setDefault(1); 
}

// OR 

$a = new \Phalcon\Forms\Element\Radio('prepaytype_a', [
    'name' => 'prepaytype',
    'value' => 1,
    'checked' => ($_POST['prepaytype'] == 1 ? 'checked' : false / or null i dont know )
]);