Sunday, July 5, 2009

Creating radio button in Zend Framework

Zend Framework Form: working with radio buttons
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’);

8 comments:

  1. And how about outputting the radio buttons without any decoration. The setSeparator('') doesnt seem to stop a
    tag being inserted between them.
    ZF is soooooo painful, gotta hunt the web for a solution to every little thing! :(

    ReplyDelete
  2. The tag i mention above is a br tag

    ReplyDelete
  3. Hola que tal? soy nueva en esto de zend y probando lo de los radio quisiera saber si hay alguna manera de saber de que al seleccionar una opcion del radio pueda aparecer cajas de texto y con la otra opcion aparecer otras opciones.

    y que despues obtener cual de los dos esta selecionado para saber en que tabla es que tengo que guardar.

    El ejemplo que estoy usando es el registro de usuarios naturales y juridicos

    ReplyDelete
  4. What if you wanted to put a description on each of those radio buttons:

    male ()
    [p]All males choose this[/p]

    female ()
    [p]All females choose this[/p]

    Thank you!

    ReplyDelete
  5. I believe radion should be radio in the code.

    ReplyDelete
  6. Best Logo Design
    I trusted you guys and you provided me with the best result. In the future will commence several of projects with you guys and I am sure that you will fulfill our all requirements. You have won our satisfaction level and further we will surely work with you.

    ReplyDelete
  7. Hi sir I need the example of code to upload image in zend framework! thank advance...

    ReplyDelete