Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

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.



64.3k

why is that ?

$b->setDefault(2);


12.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?



64.3k
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 )
]);