试图在 Zend_Form 中完成这个自动表单,我几乎快要发疯了,我已经做了几乎所有事情,这一切都要感谢 Stackoverflow 社区。这就是我想做的,我会尽量让自己清楚(我很难解释自己-_-''):
我已经做过的事情:将每个元素标签和输入包装在一个 div 中,每个元素都有一个适当的 twitter span 类来填充一个高级 div,表单本身接收一个数据数组并自动生成它。
我需要什么:我的高级 div 是跨度:12(在布局中)我需要内容有 2 个“跨度:6”div,并且每个 div 都包含相同形式的部分。
我想出的解决方案:
我的解决方案之一是单独获取每个元素并手动放置在每个 span6 div 中。另一种是在我的函数中创建一个“容器”参数,这样生成器就可以做到这一点(但我不知道如何使用 span 类创建 2 个 div 并将元素附加到相应的类中)
我希望你们明白我想要做什么以及我需要什么,谢谢=)
form/Usuario:
<?php
class Admin_Form_Usuario extends Zene_Form_Helper_Abstract
{
protected $campo = null;
/**
* Initialize form (used by extending classes)
*
* @return void
*/
public function init()
{
//Valores dos Campos
$model = new Admin_Model_Usuario();
$option = $model->getOptions();
$modelPage = new Admin_Model_Page();
$optionPage = $modelPage->getOptions();
//Atributos dos Campos
$this->campo = array(
array('Tipo'=>'Text', 'Id'=>'UsuId', 'Label'=>'Código do Usuário', 'Span' =>'6', 'Required'=>'true', 'Table'=>'Usuario','Field'=>'UsuId','Atributo'=>array('readonly'=>'true')),
array('Tipo'=>'Select', 'Id'=>'UsuFunId', 'Label'=>'Funcionário', 'Span' =>'6', 'Required'=>'true', 'Table'=>'Usuario','Field'=>'UsuFunId','Option'=>array(1=>'Guest',2=>'Charles')),
array('Tipo'=>'Text', 'Id'=>'UsuLogin', 'Label'=>'Login do Usuário', 'Span' =>'12', 'Required'=>'true', 'Table'=>'Usuario','Field'=>'UsuId'),
array('Tipo'=>'Pass', 'Id'=>'UsuSenha', 'Label'=>'Senha Usuário', 'Span' =>'6', 'Required'=>'true', 'Table'=>'Usuario','Field'=>'UsuSenha'),
array('Tipo'=>'Pass', 'Id'=>'ConfUsuSenha', 'Label'=>'Confirmar Senha', 'Span' =>'6', 'Required'=>'true', 'Table'=>'Usuario','Field'=>'ConfUsuSenha'),
array('Tipo'=>'Check', 'Id'=>'full_permission','Label'=>'Permissão Total', 'Span' =>'6', 'Required'=>'true', 'Table'=>'Usuario','Field'=>'full_permission'),
array('Tipo'=>'Check', 'Id'=>'UsuAtivo', 'Label'=>'Ativo', 'Span' =>'6', 'Required'=>'true', 'Table'=>'Usuario','Field'=>'UsuAtivo'),
array('Tipo'=>'Date', 'Id'=>'UsuDtAtivo', 'Label'=>'Data ativação', 'Span' =>'6', 'Required'=>'true', 'Table'=>'Usuario','Field'=>'UsuDtAtivo','Atributo'=>array('readonly'=>'true')),
array('Tipo'=>'Date', 'Id'=>'UsuDtDesativo', 'Label'=>'Data Desativação', 'Span' =>'6', 'Required'=>'true', 'Table'=>'Usuario','Field'=>'UsuDtDesativo','Atributo'=>array('readonly'=>'true')),
array('Tipo'=>'Select', 'Id'=>'Parent_id', 'Label'=>'Grupo', 'Span' =>'6', 'Required'=>'true', 'Table'=>'Usuario','Field'=>'Parent_id','Option'=>$option),
//array('Tipo'=>'MSelect','Id'=>'Parent_id', 'Label'=>'Grupo', 'Span' =>'6', 'Required'=>'true', 'Table'=>'Usuario','Field'=>'Parent_id','Option'=>$option),
array('Tipo'=>'Submit', 'Id'=>'save', 'Label'=>'Salvar', 'Span' =>'12' )
);
parent::createElemento($this->campo);
}
Helper Abstract (gera o form) e o executa o `parent::createElemento()`
<?php
abstract class Zene_Form_Helper_Abstract extends Zend_Form{
/**
* Metodo para retornar um lemento criado apartir de um array
* deve seguir a seguinte estrutura;
* @see Zend_Form::getElement()
*/
public function createElemento(array $options){
$element = null;
foreach ($options as $option)
{
switch ($option['Tipo']) {
case 'Hidden':
$element = new Zend_Form_Element_Hidden($option['Id']);
break;
case 'Select':
$element = new Zend_Form_Element_Select($option['Id']);
$element->addMultiOptions($option['Option']);
break;
case 'Date':
$element = new Zend_Form_Element_Text($option['Id']);
$element->setAttrib("class", "datepicker");
break;
case 'Check':
$element = new Zend_Form_Element_Checkbox($option['Id']);
break;
case 'MCheck':
$element = new Zend_Form_Element_MultiCheckbox($option['Id']);
$element->addMultiOptions($option['Option']);
break;
case 'Text':
$element = new Zend_Form_Element_Text($option['Id']);
break;
case 'Pass':
$element = new Zend_Form_Element_Password($option['Id']);
break;
case 'Checkbox':
$element = new Zend_Form_Element_Checkbox($option['Id']);
break;
case 'Textarea':
$element = new Zend_Form_Element_Textarea($option['Id']);
break;
case 'MSelect':
$element = new Zend_Form_Element_Multiselect($option['Id']);
$element->addMultiOptions($option['Option']);
break;
case 'Submit':
$elementDecorator = array(
array('ViewHelper'),
array('HtmlTag', array('tag' => 'div','style' =>'margin-left:0', 'class' => 'span'.$option['Span'])),
);
$element = new Zend_Form_Element_Button($option['Id']);
$element->setLabel($option['Label'])
->setAttrib('type','submit')
->setAttrib('class','btn btn-primary')
->setDecorators($elementDecorator);
break;
default:
break;
}
$elementDecorator = array(
array('ViewHelper'),
array('Label'),
array('HtmlTag', array('tag' => 'div','style' =>'margin-left:0', 'class' => 'span'.$option['Span'])),
);
if(isset($option['Atributo'])){
foreach ($option['Atributo'] as $key=>$value) {
$element->setAttrib($key, $value);
}
}
if(($option['Tipo'] != 'Submit') && ($option['Tipo'] != 'Button')){
$element->setLabel($option['Label'])
->setAllowEmpty((bool)$option['Required'])
->setRequired(!$option['Required'])
->addValidator('Db_NoRecordExists', false,
array(
'table' => $option['Table'],
'field' => $option['Field']
)
)->setDecorators($elementDecorator);
if ($option['Disabled'] == 'true')
$element->setAttrib('disabled','disabled');
}
$this->addElement($element);
}
}
no usuario/controller:
$form = new Admin_Form_Usuario();
no usuario/view:
$this->view->form = $form;