Zend Form + Twitter bootstrap:创建2个跨度以显示相同的表单

时间:2012-12-15 13:35:43

标签: php zend-framework twitter-bootstrap zend-form zend-decorators

我几乎疯了,试图在Zend_Form中完成这个自动化表单,我已经做了几乎所有事情,并且全部归功于Stackoverflow社区。这就是我想做的事情,我会尽力让自己清楚(我很难解释自己-_-''):

我已经做过的事情: 将每个元素标签包装并输入div中,每个元素都有一个正确的twitter span类来填充高级div,表单本身接收一个数据数组并自动生成它。

我需要什么: 我的高级Div是跨度:12(布局中) 我需要内容有2个“Span:6”div,每个div用于保存相同形式的部分。

我提出的解决方案:

我的一个解决方案是单独获取每个元素并手动放置在每个span6 div中。 另一种是在我的函数中创建一个'container'参数,所以生成器可以这样做(但我不知道如何使用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; 

1 个答案:

答案 0 :(得分:1)

如果你想制作bt表格,你可以使用ViewScript装饰器,或像这样的somting

$pro_name = new Zend_Form_Element_Text('pro_name');
    $pro_name->setLabel("Project name")
            ->setRequired(true)
            ->addValidators(array($pnalnum,$pnlength))
            ->setAttrib("class", "textInput")
            ->setDecorators(array(
                'ViewHelper',
                array(array('control' => 'HtmlTag'), array('tag' => 'div', 'class' => 'controls')),
                array('Label', array('class' => 'control-label')),
                array(array('controls' => 'HtmlTag'), array('tag' => 'div', 'class' => 'control-group'))  
            ));

这很简单,创建html:

<div class="control-group">
   <label for="pro_name" class="control-label required">Project name</label>
   <div class="controls">
      <input type="text" name="pro_name" id="pro_name" value=""class="textInput">
   </div>
</div>