2011-06-15 62 views
2

我忙於一個基於zend框架的新項目。我創建了以下表單:Zend 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' 
       ) 
      ) 
     ) 
     )); 
     $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 ID,Consumer kvk number。當用戶切換到管理單選按鈕時,這個字段必須消失(所以它必須是JS)

有沒有一種方法可以在Zend Form中直接使用?或者我必須製作自己的HTML表單?

湯姆

回答

1

你可以讓這樣的事情:

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

在控制器,你可以得到表單數據,並將其傳遞到窗體->init($data);

相關問題