开发者

Zend form: Show elements when radio has correct value

开发者 https://www.devze.com 2023-03-12 23:21 出处:网络
I am busy with a new project based on zend framework. I have created the following form: <?php class Application_Form_User extends Zend_Form

I am busy with a new project based on zend framework. I have created the following form:

<?php
class Application_Form_User extends Zend_Form
{

    public function init()
    {
       $this->setMethod('post');
       $this->setAttrib('class','zf');
       $this->addElement('text', 'username', array(
        'label' => 'Gebruikersnaam:',
        'required' => true,
        'filters' => array('StringTrim'),
        'validators'=>array(
                array('Db_NoRecordExists',
                false,
                array(
                    'table'=>'user',
                    'field'=>'username'
                )
            ))
       ));
       $this->addElement('text', 'name', array(
        'label' => 'Volledige naam:',
        'required' => true,
        'filters' => array('StringTrim'),
       ));
       $this->addElement('text', 'email', array(
        'label' => 'Email:',
        'required' => true,
        'filters' => array('StringTrim'),
        'validators'=>array(
            'EmailAddress',
            array(
                'Db_NoRecordExists',
                false,
                array(
                    'table'=>'user',
                    'field'=>'email'
                )
            )
        )
       ));
       $thi开发者_Python百科s->addElement('password', 'password1', array(
        'label' => 'Wachtwoord:',
        'required' => true,
        'filters' => array('StringTrim'),
       ));
       $this->addElement('password', 'password2', array(
        'label' => 'Wachtwoord (controle):',
        'required' => true,
        'filters' => array('StringTrim'),
        'validators'=>array(array('Identical',false,'password1'))
       ));
       $this->addElement('radio','type',array(
            'label'=>'Gebruikers type:',
            'required'=>true,
            'multiOptions'=>array(
                'consumer'=>'Klant',
                'admin'=>'Beheerder'
            )   
        ));
       $this->addElement('text', 'mobile', array(
        'label' => 'Mobiel:',
        'required' => true,
        'filters' => array('StringTrim'),
       ));
        $this->addElement('textarea', 'address', array(
        'label' => 'Address:',
        'required' => true,
        'style'=>'width: 200px;height: 100px;'
       ));

       $this->addElement('submit', 'submit', array(
        'ignore'=>true,
        'label'=>'Toevoegen'
       ));
       $this->addElement('hash', 'csrf', array(
            'ignore' => true,
        ));

    }
}

This form has a radio button with the values 'Consumer' and 'Admin'. What i want is that when the value is 'Consumer' some extra fields will be shown, and when it is 'admin', other elements will be shown.

So when the value is Consumer I want as example those fields: Consumer ID, Consumer kvk number. When the user switches to the admin radio button this fields must disapear.(So it must is JS)

Is there a way to do this in Zend Form out-of-the-box? Or must i make my own HTML form?

Tom


You can make something like this:

public function init($data = false)
{
   if (isset($data['type']) && $data['type'] == 'consumer') {
      // add element or hide element
   }
}

In the Controller you can get form data and pass it to the form ->init($data);

0

精彩评论

暂无评论...
验证码 换一张
取 消