2010-08-30 42 views
1

我有一些zend的形式。這是我的代碼:Zend窗體。添加一個href並在div中放置一些元素

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; 
} 

我的任務是把$標題和描述在一個DIV,並把$ total_assoc和$ qualif_time在其他分區。我應該在這個div之前插入href(鏈接)。我試圖用addDisplayGroup()來完成它,但它創建了一個字段集。我需要div。

Thx。

+1

我創建了我自己的'Zend_Form_Element'來解決這類問題。我稱之爲「Content」,它允許我在表單中添加Html。這對於使用Html製作標籤也很有用,因爲默認的Zend Label裝飾器會轉義Html字符。 – 2010-08-31 21:29:03

+0

是的。我試過這個。謝謝。這是工作。 – pltvs 2010-09-01 06:32:26

回答

1

嘗試使用表單裝飾。 *

要設置形式docorator你有這樣的事情添加到您的表單對象

$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> 

如果這是對你而言不夠。你必須使用decoradors每個字段:

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

裝飾現場工作方式類似。

+0

小心,簡短的標籤Zend的政策已經改變。你不應該再使用它了。 比較http://framework.zend.com/issues/browse/ZF-6343 – LittleBigDev 2012-10-04 16:11:57

相關問題