我遇到zend_form
和zend_decorator
的问题。
我创建了一个装饰器类来默认所有表单都使用列表元素,但它似乎没有用!
基本上my_decorator_design
扩展zend_form
然后我的表单扩展了装饰器。
想法?
class My_Decorator_Design extends Zend_Form {
public function loadDefaultDecorators() {
$this->addDecorator('FormElements')
->addDecorator('HtmlTag', array('tag' => 'ul')) //this adds a <ul> inside the <form>
->addDecorator('Form');
$this->setElementDecorators(array(
'ViewHelper',
'Label',
'Errors',
new Zend_Form_Decorator_HtmlTag(array('tag' => 'li')) //wrap elements in <li>'s
));
$this->setDisplayGroupDecorators(array(
'FormElements',
'Fieldset',
new Zend_Form_Decorator_HtmlTag(array('tag' => 'li')), //wrap groups in <li>'s too
new Zend_Form_Decorator_HtmlTag(array('tag' => 'ul'))
));
$this->setDisplayGroupDecorators(array(
'FormElements',
'Fieldset',
new Zend_Form_Decorator_HtmlTag(array('tag' => 'li')) //wrap groups in <li>'s too
));
}
}
class Forms_User_Update extends My_Decorator_Design {
public function __construct($options=array()) {
parent::__construct($options);//if we ever want to pass on things to zend_form
$this->setName('user_update');
$this->loadDefaultDecorators();
//user_name, first_name, email, password, date_of_birth
$user_name = new Zend_Form_Element_Text('user_name');
$first_name = new Zend_Form_Element_Text('first_name');
$email = new Zend_Form_Element_Text('email');
$password = new Zend_Form_Element_Password('password');
$password2 = new Zend_Form_Element_Password('password2');
$submit = new Zend_Form_Element_Submit('Submit');
$user_name->setRequired(true)
->setLabel('Username');
$first_name->setRequired(false)
->setLabel('First Name');
$email->setRequired(true)
->setLabel('Email:')
->addFilter('StringToLower')
->addValidator('NotEmpty', true)
->addValidator('EmailAddress');
$password->setLabel('Password:')
->setRequired(false)
->setIgnore(false)
->addValidator('stringLength', false, array(6));
$password2->setLabel('Confirm Password:')
->setRequired(false)
->setIgnore(true);
$submit->setLabel("Submit")
->setIgnore(true);
$this->addElements(array(
$user_name, $first_name, $email, $password, $password2, $submit
));
//$this->Submit->removeDecorator('Label');
//$this->addElementPrefixPath('My_Decorator', 'My/Decorator/', 'decorator');
$this->setMethod('post');
$this->setAction('/update-account');
}
}
答案 0 :(得分:3)
您在构造函数中调用loadDefaultDecorators
,然后从那里调用setElementDecorators
。
但addElement
仅在从字符串构造元素时才使用元素装饰器,而不是在传递现成元素时使用元素装饰器,而setElementDecorators
仅为已存在的控件设置装饰器(不在构造函数)。
当您首先创建元素然后将它们作为元素传递时,元素装饰器永远不会被设置。
删除构造函数中对loadDefaultDecorators
的调用。
答案 1 :(得分:1)
我也面临过这些类型的问题,但最后我为文件元素和其他输入元素定制了zend表单。
使用以下代码将文件装饰器与zend表单中的其他元素一起应用。
<?php
public function init()
{
// Set the method for the display form to POST
$this->setMethod('post');
$this->setElementDecorators(array(
'ViewHelper',
array('HtmlTag',array('tag' =>'div' ,'class'=>'field')),
array('Label',array('tag' =>'div','class'=>'label')),
));
// Add an email element
$this->addElement('text','name',array('label'=>'Name:',
'required'=>true,
$this->addElement('text','username',array('label'=>'User Name:','id'=>'user_name',
'required'=>true,
));
$this->addElement('password','password',array('label'=>'Password:',
'required'=>true,
));
$this->addElement('password','cpassword',array('label'=>'Password Again:',
'required'=>true,
));
$this->addElement('text','phone',array('label'=>'Phone:',
'required'=>true,
));
$this->addElement('text', 'email', array(
'label' => 'Email:',
'value' =>'test',
));
$this->addElement('file', 'photo', array(
'label' => 'Profile Photo:',
'decorators' =>array('file',array('HtmlTag',array('tag'=>'div','class'=>'field'))
,array('Label',array('tag'=>'div','class'=>'label')))
));
$this->addElement('submit', 'save',array('label'=>'Register Me','class'=>'submitbtn','style'=>'margin-left:150px;'));
}
?>
我希望这样可以正常工作