Thursday, 17 January 2013

Creating radio button in Zend Framework | Zend Framework Tutorial pdf

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’);

No comments: