Zend Framework Form: working with radio buttons

5 Mar

I have already discussed creation of Zend_From in my previous articles. In this article I’d discuss how to work with radio buttons in Zend Framework Form.

In your Zend_Form, radio buttons can be created using two methods.

The first one is

<?php

class CustomForm extends Zend_Form

{

public function init()

{

$this->setMethod(‘post’);

$this->setAction(‘user/process’);

$gender = $this->createElement(‘radion’,’gender’);

$gender->setLabel(‘Gender:’)

->addMultiOptions(array(

‘male’ => ‘Male’,

‘female’ => ‘Female’

))

->setSeparator(”);

}

}

In the above code we first create our form by extending it from Zend_Form, override its init() method and setting its method and action attributes.

Next we create our radio button as

$gender = $this->createElement(‘radion’,’gender’);

Here first argument specify that we want to create radion button element of the form and the second argument set the name

and id of the radio button element group. In the next line we call addMultiOptions() method giving its array of optional

values. and lost but not least, I am calling method setSeparator for placing both radio buttons on the same line. If this

setSeparator method is not called, each radion button will appear on separate line.

The above radio button can also be created as

<?php

class CustomForm extends Zend_Form

{

public function init()

{

$this->setMethod(‘post’);

$this->setAction(‘user/process’);

$gender = new Zend_Form_Element_Radio(‘gender’);

$gender->setLabel(‘Gender:’)

->addMultiOptions(array(

‘male’ => ‘Male’,

‘female’ => ‘Female’

))

->setSeparator(”);

}

}

The only difference in the above two example is that I have placed

$gender = $this->createElement(‘radion’,’gender’);

With

$gender = new Zend_Form_Element_Radio(‘gender’);

Both statements give the same result.

For setting different radio button options, you can also use the following method.

$gender->addMultiOption(‘male’,’Male’);

$gender->addMultiOption(‘female’,’Female’);

4 Responses to “Zend Framework Form: working with radio buttons”

  1. tanmay August 19, 2009 at 11:50 am #

    what if i want one option of the radio button be selected amongsts two or three?

  2. Yasir Khan February 24, 2010 at 11:15 am #

    ->setAttrib(“checked”,”checked”)
    this will checked the last value of multiOptions.
    Like
    $gender->setLabel(‘Gender:’)

    ->addMultiOptions(array(

    ‘male’ => ‘Male’,

    ‘female’ => ‘Female’

    ))
    ->setAttrib(“checked”,”checked”)
    ->setSeparator(”);
    IN this case female radio button will be checked by default.

  3. m4niac July 31, 2010 at 7:02 am #

    ->setValue(‘the_key_of_the_value_in_the_list_of_options’);

    so if you want male to be checked: ->setValue(‘male’);

  4. Gerard Vogels November 17, 2010 at 8:00 pm #

    Hai

    My form has to be in a table format so I want the html output of the radio element to be placed in a table row. For various other elements like text and textarea controls this is possible with the use of the appropriate decorators. Somehow this doesn’t seem to work for radiobutton elements. Any idea’s???

    Gerard

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: