【问题标题】:Zend form: Show elements when radio has correct valueZend 形式:当单选具有正确值时显示元素
【发布时间】:2011-06-15 12:36:22
【问题描述】:

我正忙于一个基于 zend 框架的新项目。我创建了以下表单:

<?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'
                )
            )
        )
       ));
       $this->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,
        ));

    }
}

此表单有一个单选按钮,其值为“Consumer”和“Admin”。我想要的是,当值为“消费者”时,将显示一些额外的字段,当值为“管理员”时,将显示其他元素。

因此,当值为 Consumer 时,我希望这些字段作为示例:Consumer ID、Consumer kvk number。当用户切换到管理单选按钮时,此字段必须消失。(所以它必须是 JS)

有没有办法在 Zend Form 中开箱即用地做到这一点?还是我必须自己制作 HTML 表单?

汤姆

【问题讨论】:

    标签: php zend-framework forms zend-form


    【解决方案1】:

    你可以做这样的事情:

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

    在Controller中可以获取表单数据并传递给表单-&gt;init($data)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-11
      • 1970-01-01
      相关资源
      最近更新 更多