基于条件的Zend表单元素

I have came across with one problem i need to show different form elements

  • Based on different condition to the user i know it will be easy in core php but want to do in zend environment

EXAMPLE:

If person is disabled we will show two radio buttons

$disable = new Shaadi_Form_Element_radio('disablitiy');
$disableArr = array(""=>" Doesn't Matter","Y"=>" show disabled");

IF he is not disabled

$disable = new Shaadi_Form_Element_radio('disablitiy');
$disableArr = array(""=>" Doesn't Matter","N"=>" do not show disabled");

I want this code to be done in form how can I optimize this please help me

Whenever a form - or any object, for that matter - is dependent upon some information external to the form, I usually pass that information in the form's constructor. Then I inspect it later in init() when I am building the form.

Example:

class My_Form extends Zend_Form
{
    protected $hasDisability;

    public function __construct($hasDisability)
    {
        $this->hasDisability = (bool) $hasDisability;
        parent::__construct();
    }

    public function init()
    {
        // Add all your other elements
        // Blah, blah

        // Add the element that is dependent upon the $hasDisability value
        $disable = new Shaadi_Form_Element_radio('disablitiy');
        $disableArr = $this->hasDisability
            ? array(""=>" Doesn't Matter","Y"=>" show disabled")
            : array(""=>" Doesn't Matter","N"=>" do not show disabled");

        // Add the $disableArr into the radio element
        $disable->setMultiOptions($disableArr); 
    }
}

Usage - perhaps in a controller - is then something like:

$form = new My_Form(true);  // for a disabled used
$form = new My_Form(false);  // for a non-disabled user

To implement the element in form, based on some condition, you need to pass the user disability option in form constructor through controller. You can perform below steps to display radio button in view:

Form code:

 class Application_Form_User extends Zend_Form {
        public function init() {
            $formAttribs = $this->getAttribs();
            $isDisable   = $formAttribs['disable'];

            if (!$disabled) {
               $disableArr = array(""=>" Doesn't Matter","Y"=>" show disabled");
            } else {
              $disableArr = array(""=>" Doesn't Matter","N"=>" do not show disabled");
            }

            $this->addElement('radio', 'disability', array(
                              'label' => 'Disability',
                              'required' => true,
                              'multiOptions' => $disableArr,                
            ));

        }
    }

In controller:

    public function addAction() {
        $disable = ($user->disable) ? true : false;
        $form = new Application_Form_User(array('disable' => $disable));
        $this->view->form = $form;
    }

In view you can get 'disability' element as below :

  <?php echo $this->form->getelement('disability'); ?>

In your form, define your radio element:

$disable = new Shaadi_Form_Element_radio('disablitiy');

In your Controller, call this element and add the good options:

if ($disable)
    $disableArr = array(""=>" Doesn't Matter","Y"=>" show disabled");
else
    $disableArr = array(""=>" Doesn't Matter","N"=>" do not show disabled");

$form->getElement('disablitiy')->setMultiOptions($disableArr);