【问题标题】:Zend Form. Adding a href and placing some elements in divZend 形式。添加一个href并将一些元素放在div中
【发布时间】:2010-08-30 06:45:30
【问题描述】:

我有一些 zend 形式。这是我的代码:

private function _createForm($action) {

    $form = new Zend_Form();

    $form->setName($action . '_form');
    $form->setMethod('post');

    // Main tab
    $title = $form->createElement('text', 'title');
    $title->setLabel('Title')
          ->setAttrib('maxlength',50)->setAttrib('id', 'title')->setAttrib('class', $action . '_title')
          ->setAttrib('style','height: 15px; width: 200px;')
          ->setRequired(true)
          ->setDecorators(array(
            'ViewHelper',
            array(array('data' => 'HtmlTag'), array('tag' => 'td', 'class'  => 'element')),
            array('Label', array('tag' => 'td')),
            array(array('row' => 'HtmlTag'), array('tag' => 'tr')),
          ));

    $description = $form->createElement('textarea', 'description');
    $description->setLabel('Description')
                ->setAttrib('style','height: 50px; width: 200px;')->setAttrib('id', 'description')->setAttrib('class', $action . '_description')
                ->setDecorators(array(
                  'ViewHelper',
                  array(array('data' => 'HtmlTag'), array('tag' => 'td', 'class'  => 'element')),
                  array('Label', array('tag' => 'td')),
                  array(array('row' => 'HtmlTag'), array('tag' => 'tr')),
                ));
    // Advanced tab
    $qualif_time = $form->createElement('text', 'qualif_time');
    $qualif_time->setLabel('Qualification Time')
        ->setAttrib('maxlength',11)->setAttrib('id', 'qualif_time')->setAttrib('class', $action . '_qualif_time')->setAttrib('style','height: 15px; width: 200px;')
        ->setDecorators(array(
          'ViewHelper',
          array(array('data' => 'HtmlTag'), array('tag' => 'td', 'class'  => 'element')),
          array('Label', array('tag' => 'td')),
          array(array('row' => 'HtmlTag'), array('tag' => 'tr')),
        ));
    $total_assoc_down = $form->createElement('text', 'total_assoc_down');
    $total_assoc_down->setLabel('Total Associates Downline')
        ->setAttrib('maxlength',11)->setAttrib('id', 'total_assoc_down')->setAttrib('class', $action . '_total_assoc_down')->setAttrib('style','height: 15px; width: 200px;')
        ->setDecorators(array(
          'ViewHelper',
          array(array('data' => 'HtmlTag'), array('tag' => 'td', 'class'  => 'element')),
          array('Label', array('tag' => 'td')),
          array(array('row' => 'HtmlTag'), array('tag' => 'tr')),
        ));

    $submit = $form->createElement('submit', $action);
    $submit->setAttrib('id', 'submit')->setAttrib('value', $action)
           ->setDecorators(array(
             'ViewHelper',
             array(array('data' => 'HtmlTag'), array('tag' => 'td', 'class'  => 'element')),
             array(array('row' => 'HtmlTag'), array('tag' => 'tr')),
           ));

    $form->addElements(array(
        $title, $description, $qualif_time, $total_assoc_down
    ));

    $form->addDisplayGroup(array('qualif_time', 'total_assoc_down'), 'advanced_tab');
    $advanced_tab = $form->getDisplayGroup('advanced_tab');
    $form->addElements(array($advanced_tab, $submit));

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

我的任务是将 $title 和 description 放在一个 div 中,并将 $total_assoc 和 $qualif_time 放在另一个 div 中。我应该在这个 div 之前插入 href(link)。我试图用 addDisplayGroup() 来做,但它创建了一个字段集。我需要 div。

谢谢。

【问题讨论】:

  • 我创建了自己的Zend_Form_Element 来解决此类问题。我称它为Content & 它允许我在表单中我需要的任何地方添加 Html。这对于制作带有 Html 的标签也很有用,因为默认的 Zend Label 装饰器会转义 Html 字符。
  • 是的。我试过这个。谢谢。这是工作。

标签: php zend-framework html zend-form


【解决方案1】:

尝试使用表单装饰器。*

要设置表单文档,您必须在表单对象中添加类似这样的内容

$decoratorFile = "path to decoration phtml for example user/" path starts automatic from views/scripts

$paramsArr = array('viewScript' => $decoratorFile);
$decorator = new Zend_Form_Decorator_ViewScript($paramsArr);

$this->setDecorators(array($decorator));  // $this is a your form object

现在您必须为所有表单元素准备一个 phtml:

<form class="formU" enctype="application/x-www-form-urlencoded"
    action="<?= $this->element->getAction() ?>"
    method="<?= $this->element->getMethod() ?>"
    name="<?= $this->element->getName() ?>"
    id="<?= $this->element->getId() ?>">

    <?php 
    // all field in foreach
    $formElements = $this->element->getElements();
    foreach ($formElements as $formElement) {
        echo $formElement;
    }

    // or you can use something like this for each field

    $this->element->getElement('elementName')

    ?>
</form>

如果这对您来说还不够。您必须为每个字段使用装饰器:

http://framework.zend.com/manual/en/zend.form.decorators.html

字段装饰器的工作方式类似。

【讨论】:

猜你喜欢
  • 2023-03-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多