使用zend形式添加额外元素

时间:2012-12-20 11:57:52

标签: php zend-framework

这是我的代码

    private $elementDecorators = array(
        'ViewHelper',
        'Errors',
        array(array('data' => 'HtmlTag'), array('tag' => 'td')),
        array('Label', array('tag' => 'td','class'=>'blue-color','placement'=>'prepend')),
        array(array('row' => 'HtmlTag'), array('tag' => 'tr')),

    );
     public function init()
     {
      $username  = new Zend_Form_Element_Text('username',array(
            'decorators'    =>$this->elementDecorators,
            'label'         =>'Username',
            'required'      =>true,
            'span'          =>array('class'=>'validation','id'=>'unameInfo'),

        ));
    }

       $this->addElements(array(
                        $username
        ));
        $this->setDecorators(array(
           'FormElements',
            array('HtmlTag',
                array('tag'=>'table', 'width' => '100%')
                ),
               'Form'
        ));

为上述代码创建的表单如下

   <tr>
       <td id="username-label"><label for="username" class="blue-color required">Username</label></td>
       <td><input type="text" name="username" id="username" value="" span="Array"></td>
   </tr>

我想要关注html

<tr>
           <td id="username-label"><label for="username" class="blue-color required">Username</label></td>
           <td>
               <input type="text" name="username" id="username" value="" span="Array">
               <span class="validation" id="userinfo"></span>
           </td>
</tr>

如何在上面的zend表单代码中添加span标记?

提前谢谢

2 个答案:

答案 0 :(得分:0)

尝试如下,

...
     $submit = new Zend_Form_Element_Submit('submit', array(
                    'label' => 'Submit Button',
                    'class' => 'form-submit',
                    'decorators' => array(
                        'ViewHelper',
                    ),
                ));

        //$submit->removeDecorator('Label');
        $this->addElement($submit);

        $reset = new Zend_Form_Element_Reset('reset', array(
                    'label' => 'Reset Button',
                    'class' => 'form-reset',
                    'decorators' => array(
                        'ViewHelper',
                    ),
                ));
        //$submit->removeDecorator('Label');
        $this->addElement($reset);

        $this->addDisplayGroup(array('submit', 'reset',), 'submitButtons', array(
            'order' => 10,
            'decorators' => array(
                'FormElements',
                array(
                    array('data' => 'HtmlTag'),
                    array('tag' => 'td','class'=>'move_td')
                ),
                array(
                    array('row' => 'HtmlTag', 'class' => 'element'),
                    array('tag' => 'tr')
                )
            ),
        ));
...

这将生成如下代码,

...
<td class="move_td">

<input type="submit" class="form-submit" value="Submit Button" id="submit" name="submit">

<input type="reset" class="form-reset" value="Reset Button" id="reset" name="reset">
</td>
...

已更新

使用以下代码生成范围

$this->addElement(
        'hidden',
        'dummy',
        array(
            'required' => false,
            'ignore' => true,
            'autoInsertNotEmptyValidator' => false,
            'decorators' => array(
                array(
                    'HtmlTag', array(
                        'tag'  => 'span',
                        'id'   => 'span-id',
                        'class' => 'span-class'
                    )
                )
            )
        )
    );
    $this->dummy->clearValidators(); 

现在你需要使用addDisplayGroup函数来分组html元素。

答案 1 :(得分:0)

您可以使用AnyMarkup装饰器。

$username  = new Zend_Form_Element_Text('username',array(
     'decorators'    => array(
        'ViewHelper',
         array('AnyMarkup', array('markup' => 'your-markup-here', 'placement' => 'append')),
        'Errors',
        array(array('data' => 'HtmlTag'), array('tag' => 'td')),
        array('Label', array('tag' => 'td','class'=>'blue-color','placement'=>'prepend')),
        array(array('row' => 'HtmlTag'), array('tag' => 'tr')),
     ),
    'label'    => 'Username',
    'required' => true,
    'span'     => array('class'=>'validation','id'=>'unameInfo'), 
    // actually, this last 'span' entry strikes me as odd
));

要使用短格式(如上所述)将装饰器添加到元素而不是创建实例,您需要使用元素注册装饰器的路径/前缀,例如:

$username->addPrefixPath('My_Decorator_', APPLICATION_PATH . '/../library/My/Decorator', Zend_Form_Element::DECORATOR);

您可以使用以下命令将该前缀/路径添加到所有(当前定​​义的)元素:

$form->addElementPrefixPath($prefix, $path)

方法