Consultez la FAQ sur le ZF avant de poster une question
Vous n'êtes pas identifié.
Pages: 1
Bonjour,
Voici j'ai un Zend_Form rempli à partir des informations d'un tableau (exemple tiré de ce site : http://davidcaylor.com/2008/03/24/build … zend_form/) :
$array [0][ 'id' ] = 1005; $array [0][ 'title' ] = 'Mr.' ; $array [0][ 'first' ] = 'Ted' ; $array [0][ 'last' ] = 'Smith' ; $array [1][ 'id' ] = 1006; $array [1][ 'title' ] = 'Mr.' ; $array [1][ 'first' ] = 'Mark' ; $array [1][ 'last' ] = 'Jones' ; $array [2][ 'id' ] = 1007; $array [2][ 'title' ] = 'Ms.' ; $array [2][ 'first' ] = 'Sally' ; $array [2][ 'last' ] = 'Adams' ; |
$form = new Zend_Form(); $form ->setMethod( 'post' ) ->setAttrib( 'id' , 'contactForm' ); $subForm = new Zend_Form_SubForm(); foreach ( $array as $rownum => $row ){ $id = $row [ 'id' ]; $rowForm = new Zend_Form_SubForm(); foreach ( $row as $key => $value ){ if ( $key == 'id' ) continue ; $rowForm ->addElement( 'text' , $key , array ( 'value' => $value , ) ); } $rowForm ->setElementDecorators( array ( 'ViewHelper' , 'Errors' , array ( 'HtmlTag' , array ( 'tag' => 'td' )), )); $subForm ->addSubForm( $rowForm , $id ); } $subForm ->setSubFormDecorators( array ( 'FormElements' , array ( 'HtmlTag' , array ( 'tag' => 'tr' )), )); $form ->addSubForm( $subForm , 'contacts' ); $form ->setSubFormDecorators( array ( 'FormElements' , array ( 'HtmlTag' , array ( 'tag' => 'tbody' )), )); $form ->setDecorators( array ( 'FormElements' , array ( 'HtmlTag' , array ( 'tag' => 'table' )), 'Form' )); $form ->addElement( 'submit' , 'submit' , array ( 'label' => 'Submit' )); $form ->submit->setDecorators( array ( array ( 'decorator' => 'ViewHelper' , 'options' => array ( 'helper' => 'formSubmit' )), array ( 'decorator' => array ( 'td' => 'HtmlTag' ), 'options' => array ( 'tag' => 'td' , 'colspan' => 4) ), array ( 'decorator' => array ( 'tr' => 'HtmlTag' ), 'options' => array ( 'tag' => 'tr' )), )); |
Voici ce qui est créé en HTML (pour l'exemple) :
< table > // Valeur associées < tr >< td > Valeur 1.1 </ td > < td > Valeur 1.2 </ td > .... </ tr > < tr >< td > Valeur 2.1 </ td > < td > Valeur 2.2 </ td > ..... </ tr > </ table > |
Cet exemple affiche bien un formulaire avec un sous formulaire pour chaque ligne du tableau.
Le problème étant : comment ajouter la première ligne afin d'afficher les headers ? :
// Libéllés des colonnes du tableau < tr >< th >Intitulé 1</ th > < th >Intitulé 2</ th > ... </ tr > // Valeur associées < tr >< td > Valeur 1.1 </ td > < td > Valeur 1.2 </ td > .... </ tr > < tr >< td > Valeur 2.1 </ td > < td > Valeur 2.2 </ td > ..... </ tr > |
Merci d'avance pour toute réponse
Hors ligne
Solution (trouvé sur : http://paveldubinin.com/2011/04/real-ta … zend-form/) :
Ajouter une classe "decorator" : /library/Form/Decorator/SimpleTable.php
<?php class Form_Decorator_SimpleTable extends Zend_Form_Decorator_Abstract { public function render ( $content ) { $element = $this ->getElement(); $view = $element ->getView(); if (null === $view ) { return $content ; } $columns = $this ->getOption( 'columns' ); $class = $this ->getOption( 'class' ); $id = $this ->getOption( 'id' ); $columns_html = '' ; foreach ( $columns as $current_column_name ) { $columns_html .= '<th>' . $current_column_name . '</th>' ; } $result = ' <table class = "'.$class.'" id= "'.$id.'" > <thead> <tr> '.$columns_html.' </tr> </thead> <tbody> '.$content.' </tbody> </table> '; return $result ; } } |
Et remplacer la ligne
$form ->setDecorators( array ( 'FormElements' , array ( 'HtmlTag' , array ( 'tag' => 'table' )), 'Form' )); |
Par :
$form ->addPrefixPath( 'Form_Decorator' , 'Form/Decorator/' , 'Decorator' ); $form ->setDecorators( array ( 'FormElements' , array ( 'SimpleTable' , array ( 'columns' => array ( '' , 'title' , 'firstname' , 'lastname' ))), // '' pour la colomne id afin de ne rien afficher 'Form' )); |
Et le tour est joué !
Dernière modification par niusha (10-08-2011 10:19:25)
Hors ligne
Pages: 1