2010-08-30 15 views
1

J'ai une forme zend. Il est mon code:Forme de Zend. Ajouter un href et placer quelques éléments dans 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; 
} 

Ma tâche consiste à placer le titre de $ et une description dans un div, et en plaçant total_assoc de $ et de qualif_time $ dans d'autres div. Et je devrais insérer href (lien) avant ce divs. J'ai essayé de le faire avec addDisplayGroup(), mais cela crée un fieldset. J'ai besoin de div.

Thx.

+1

J'ai créé mon propre 'Zend_Form_Element' pour corriger ce genre de problème. Je l'ai appelé "Contenu" et cela me permet d'ajouter du HTML là où j'ai besoin dans le formulaire. Ceci est également utile pour créer des labels avec du HTML, car le décorateur Zend Label par défaut échappe les caractères Html. –

+0

Oui. J'ai essayé ça. Merci. C'est du travail. – pltvs

Répondre

1

Essayez d'utiliser le formulaire de décoration. *

Pour définir la forme docorator vous devez ajouter quelque chose comme ceci à votre objet formulaire

$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 

Maintenant, vous devez préparer un phtml pour tous les éléments de forme:

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

Si cela est pas assez pour toi. Vous devez utiliser decoradors pour chaque champ:

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

Décorateurs pour le champ fonctionne de la même.

+0

Faites attention, la courte étiquette ouverte La politique de Zend a changé. Vous ne devriez plus l'utiliser. cf. http://framework.zend.com/issues/browse/ZF-6343 – LittleBigDev