views:

192

answers:

4

I have the next radio button group:

$enabled = $this->createElement('radio', 'enabled')
                ->setLabel('Enabled')
                ->setMultiOptions(array('1'=>'yes', '0'=>'no'))
                ->setValue($rank_values['enabled'])
                ->setAttrib('id', 'enabled')
                ->setAttrib('class', $action . '_enabled')
                ->setSeparator('');

How can I set a checked radio? Now, when I open my script, there are not selected radio. I want to select 'yes'. How?

Thank you.

+1  A: 

Use this:

->setAttrib("checked","checked")

So that your complete code looks like this:

$enabled = $this->createElement('radio', 'enabled')
            ->setLabel('Enabled')
            ->setMultiOptions(array('0'=>'no', '1'=>'yes'))
            ->setAttrib("checked","checked")
            ->setValue($rank_values['enabled'])
            ->setAttrib('id', 'enabled')
            ->setAttrib('class', $action . '_enabled')
            ->setSeparator('');

[EDIT] Using setValue:

You can alternatively use this:

->setValue('1')

This will check the option represented by value 1 which is yes.

shamittomar
yes, it works if we have only one element. I have 2 elements in radio group.
Alexander.Plutov
Did you try the complete code that I give ?
shamittomar
Yes. It selects last element in multioptions. It selects `no`
Alexander.Plutov
@Alexander, that's why I REPLACED `yes` and `no` in my complete code. See it carefully.
shamittomar
@shamittomar, I think doing the `setAttrib` being "checked" and "checked" is setting that attribute for all elements, so the last element will generally be defaulted to the checked answer.
Brad F Jacobs
Ofcause. It will be work. But I NEED a `yes` first value. Indian programming... sorry.
Alexander.Plutov
@Alexander, then use `->setValue('1')`. This will set the `yes` as default.
shamittomar
A: 

Yeah. I have resolved it using jQuery:

jQuery(document).ready(function(){
    var $radios = $('input:radio[name=enabled]');
    if($radios.is(':checked') === false) {
        $radios.filter('[value=1]').attr('checked', true);
    }
});
Alexander.Plutov
+1  A: 

it is much more easyer :)

$enabled = $this->createElement('radio', 'enabled')
                ->setLabel('Enabled')
                ->setMultiOptions(array('1'=>'yes', '0'=>'no'))
                ->setValue($rank_values['enabled'])
                ->setAttrib('id', 'enabled')
                ->setAttrib('class', $action . '_enabled')
                ->setSeparator('')
                ->setValue("1");
Fincha
+1  A: 

I found that if you have a filter set then ->setvalue('X') does not work

I removed ->addFilter ( 'StringToLower')

and added ->setSeparator('')->setValue('N');

Worked a treat

Maxui