【发布时间】: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