0
$elementDecoration = array(
        'ViewHelper',
        'Description',
        'Errors',
        array(array('data'=>'HtmlTag'), array('tag' => 'td', 'valign' => 'TOP')),
        array('Label', array('tag' => 'td')),
        array('Errors'),
        array(array('row'=>'HtmlTag'),array('tag'=>'tr'))
            );


        $username = new Zend_Form_Element_Text('username');
        $username->setLabel('username')
        ->setDecorators($elementDecoration)
        ->setRequired(true)
        ->addFilter('StripTags')
        ->addFilter('StringTrim')
        ->addValidator('NotEmpty');

输出如下

<tr>
    <td id="username-label">
        <label for="username" class="required">username</label>
    </td>
<td valign="TOP">
    <input type="text" name="username" id="username" value="">
        <ul class="errors"><li>Value is required and can't be empty</li></ul>
    </td>
</tr>

但我想要像下面这样的输出

  <tr>
    <td>User Name:</td>
  </tr>
  <tr>
    <td colspan=2><input name="username"/></td>
  </tr>
  <tr>
    <td class="error_msg" colspan=2>Required</td>
  </tr>
  1. 我也想要和上面一样。
  2. 同样,想删除ul li。
4

1 回答 1

0

请阅读并理解以下内容:

要将表单呈现为表格:

$form->setDecorators(array(
    'FormElements',
    array('HtmlTag', array('tag' => 'table')),
    'Form'
));

另外,您需要确保元素和标签位于表格数据和表格行中:

$form->setElementDecorators(array(
    'ViewHelper',
    'Errors',
    array('decorator' => array('td' => 'HtmlTag'), 'options' => array('tag' => 'td')),
    array('Label', array('tag' => 'td')),
    array('decorator' => array('tr' => 'HtmlTag'), 'options' => array('tag' => 'tr')),
));

对于您的“提交”按钮,您需要修改装饰器,以便提交跨越两列:

 $form->submit->setDecorators(array(
        array(
            'decorator' => 'ViewHelper',
            'options' => array('helper' => 'formSubmit')),
        array(
            'decorator' => array('td' => 'HtmlTag'),
            'options' => array('tag' => 'td', 'colspan' => 2)),
        array(
            'decorator' => array('tr' => 'HtmlTag'),
            'options' => array('tag' => 'tr')),
    )); 

上面的代码将为您提供如下表。现在您可以根据需要进行编辑。您的最后一个表结构不清楚,因为第一个只有 1 列,第二个有 colspan=2。

<form>
 <table>
   <tr>
   <td>Label</td>
   <td><input type="text" name="test"></td>
  </tr>
  <tr>
   <td colspan=2><input type="submit"></td>
  </tr>
 </table>
</form> 
于 2013-03-06T09:51:18.747 回答