Précédent   Forum des professionnels en informatique > PHP > Outils > Zend > Zend Framework > Zend_Form
Zend_Form Forum d'entraide pour le composant Zend_Form du Zend Framework (création de formulaires, helpers, AJAX etc.). Avant de poster -> FAQ Zend_Form.
Partagez cette discussion sur d'autres réseaux sociaux : Viadeo Twitter Google Facebook Digg Delicious MySpace Yahoo
Réponse Proposer ce sujet en actualité
 
Outils de la discussion
Publicité
'
Vieux 09/05/2011, 10h23   #1
Membre régulier
 
Inscription : février 2011
Messages : 114
Détails du profil
Informations forums :
Inscription : février 2011
Messages : 114
Points : 77
Points : 77
Par défaut Mise en place d'un tableau - Zend_Form

Bonjour,

je travaille actuellement sur la mise en place d'un formulaire constitué de 2 sous formulaires.
Les éléments propres à mon formulaires et ceux du premier sous formulaires sont de la forme:
Libellé1: Valeur1
Libellé2: Valeur2
...

A l'aide des décorateurs, j'ai réussi à présenter ces données dans le format présenté ci-dessus.

Le problème se pose pour le deuxième sous formulaire; en effet, les données à saisir sont de la forme d'un tableau comme dans l'exemple ci-dessous:

Code :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
 
<table>
     // 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>
</table>
En fait j'ai réussi à créer le tableau mais sans les libellés de mes colonnes, je ne vois pas comment les rajouter dans mon formulaires sachant que dans ma vue je fais un "echo $this->form " pour afficher tous les éléments du formulaire et des sous formulaires.

Merci d'avance pour vos réponses...

Flilou
flilou est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 09/05/2011, 14h24   #2
Membre régulier
 
Inscription : février 2011
Messages : 114
Détails du profil
Informations forums :
Inscription : février 2011
Messages : 114
Points : 77
Points : 77
Toujours rien???

Je renouvelle ma demande car je suis bloqué:

Je génère par mon code un formulaire mis en page par tableau grâce aux decorators.
Jusque là, ça fonctionne correctement.

Méthode utilisée :
le formulaire est encapsulé dans les balises <form> et <table>
chaque ligne de la table est un subform, encapsulé dans les balises <tr>
et chaque élément des subforms est encapsulés dans les balises <td>


Ce que je souhaiterai maintenant, c'est ajouté une ligne d'en-tête du genre :
Code :
1
2
3
4
5
6
7
<tr>
 
    <th>nom colonne 1</th>
 
    <th>nom colonne 2</th>
 
</tr>
Si l'un de vous a un conseil, une idée
ça serait vraiment la bienvenue
flilou est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 11/05/2011, 14h34   #3
Membre régulier
 
Inscription : février 2011
Messages : 114
Détails du profil
Informations forums :
Inscription : février 2011
Messages : 114
Points : 77
Points : 77
Bonjour,

je suis toujours bloqué sur ce problème d'entêtes avec zend_form et apparemment personne n'a de réponses pour moi

Vous trouverez ci-dessous mon code concernant la mise en forme de mon tableau (sans les entêtes que je n'ai pas réussi à rajouter!)
Code :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
 
 
			// Construction du sous formulaire
			$sousform2 = new Zend_SubForm();			
 
 
			// Décorateur du sous formulaire 2
			$decorator2 = array ('FormElements', array ('tag' => 'tr', 'align' => 'center' ), array (array ('table' => 'HtmlTag' ), array ('tag' => 'table', 'width' => '100%', 'border' => '0' ) ), array ('Fieldset', array ('tag' => 'legend', 'class' => 'fieldsetSubForm2' ) ), array (array ('td' => 'HtmlTag' ), array ('tag' => 'td', 'colspan' => 2 ) ), array (array ('tr' => 'HtmlTag' ), array ('tag' => 'tr' ) ) );
 
 
			// Elément 1
			$decoratorElem1 = array ('ViewHelper', array ('Description', array ('tag' => 'span', 'escape' => false' ) ), array (array ('data' => 'HtmlTag' ), array ('tag' => 'td' ) ), array ('Label', array ('tag' => 'td' ) ), array ( array ( 'row' => 'HtmlTag' ), array ( 'tag' => 'tr', 'openOnly' => 'true' ) ) );
 
			$sousformElem1 = new Zend_Form_Element_Text ( 'element_1' );
			$sousformElem1->setName ( 'element_1' );
			$sousformElem1->setDescription ( 'Element 1' );
			$sousformElem1->setLabel ( 'Element 1' );
			$sousformElem1->setOrder ( 1 );
			$sousformElem1->setDecorators ( $decoratorElem1 );
			sousform2->addElement ( $sousformElem1 );
 
			// Elément 2
			$decoratorElem2 = array ('ViewHelper', array ('Description', array ('tag' => 'span', 'escape' => false) ) ), array (array ('data' => 'HtmlTag' ), array ('tag' => 'td' ) ), array ('Label', array ('tag' => 'td' ) ) );
 
			$sousformElem2 = new Zend_Form_Element_Text ( 'element_2' );
			$sousformElem2->setName ( 'element_2' );
			$sousformElem2->setDescription ( 'Elément 2' );
			$sousformElem2->setLabel( 'Elément 2' );
			$sousformElem2->setOrder ( 2 );
			$sousformElem2->setDecorators ( $decoratorElem2 );
			sousform2->addElement ( $sousformElem2 );
 
			// Elément 3
			$decoratorElem3 = array ('ViewHelper', array ('Description', array ('tag' => 'span', 'escape' => false ) ), array (array ('data' => 'HtmlTag' ), array ('tag' => 'td' ) ), array ('Label', array ('tag' => 'td' ) )  );
 
			$sousformElem3 = new Zend_Form_Element_Text ( 'element_3' );
			$sousformElem3->setName ( 'element_3' );
			$sousformElem3->setDescription ( 'Elémetn 3' );
			$sousformElem3->setLabel( 'Elément 3' );
			$sousformElem3->setOrder ( 3 );
			$sousformElem3->setDecorators ( $decoratorElem3 );
			sousform2->addElement ( $sousformElem3 );		
 
			//  Elem4
			$decoratorElem4 = array ('ViewHelper', array ('Description', array ('tag' => 'span', 'escape' => false) ), array (array ('data' => 'HtmlTag' ), array ('tag' => 'td' ) ), array ('Label', array ('tag' => 'td' ) ), array ( array ( 'row' => 'HtmlTag' ), array ( 'tag' => 'tr', 'closeOnly' => true ) ) );
 
			$sousformElem4 = new Zend_Form_Element_Text ( 'element_4' );
			$sousformElem4->setName ( 'element_4' );
			$sousformElem4->setDescription ( 'Elément 4' );
			$sousformElem4->setLabel( 'Elément 4' );
			$sousformElem4->setOrder ( 4 );
			$sousformElem4->setDecorators ( $decoratorElem4 );
			sousform2->addElement ( $sousformElem4 );
 
			$sousform2->setDecorators($decorator2);
Le premier affichage de mon sous formulaire est correcte, c'est à dire de la forme:
Code :
1
2
3
4
5
6
7
8
9
10
11
12
<table>
   <tr>
      <td> Label </td>
      <td> Elément 1 </td>
      <td> Elément 2 </td>
      <td> Elément 3</td>
      <td> Elément 4</td>
   </tr>
   <tr>
       ..
   </tr>
</table>
mais dès que la page est rafraîchis (suite à un submit de mon premier sous formulaire par exemple), la structure de mon tableau est modifiée:
Code :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<table>
   <tr>
      <td> Label </td>
      <td> Elément 1 </td>
   </tr>
   <tr>
      <td> Elément 2 </td>
      <td> Elément 3</td>
   </tr>
   <tr>
      <td> Elément 4</td>
   </tr>
   <tr>
       ..
   </tr>
</table>
Je ne vois pas d'où ça peut venir, en revanche j'espère que quelqu'un saura répondre cette fois ci...
Merci d'avance
flilou est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 13/05/2011, 09h35   #4
Membre régulier
 
Inscription : février 2011
Messages : 114
Détails du profil
Informations forums :
Inscription : février 2011
Messages : 114
Points : 77
Points : 77
Bonjour,

apparemment mon problème n'attire pas grand monde

J'ai trouvé la solution à mon deuxième problème (changement de structure de mon tableau après rafraichissement de ma page), grâce aux paramètres 'placement' dans les décorateurs des élements du formulaire.
En fait, il fallait rajouter :
Code :
 'placement' => 'prepend'
pour le premier élément de ma ligne et:
Code :
 'placement' => 'append'
pour le dernier élément de ma ligne.

ce qui donne le code suivant si cela peut aider:
Code :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
 
           // Construction du sous formulaire
            $sousform2 = new Zend_SubForm();            
 
 
            // Décorateur du sous formulaire 2
            $decorator2 = array ('FormElements', array ('tag' => 'tr', 'align' => 'center' ), array (array ('table' => 'HtmlTag' ), array ('tag' => 'table', 'width' => '100%', 'border' => '0' ) ), array ('Fieldset', array ('tag' => 'legend', 'class' => 'fieldsetSubForm2' ) ), array (array ('td' => 'HtmlTag' ), array ('tag' => 'td', 'colspan' => 2 ) ), array (array ('tr' => 'HtmlTag' ), array ('tag' => 'tr' ) ) );
 
 
            // Elément 1
            $decoratorElem1 = array ('ViewHelper', array ('Description', array ('tag' => 'span', 'escape' => false' ) ), array (array ('data' => 'HtmlTag' ), array ('tag' => 'td' ) ), array ('Label', array ('tag' => 'td' ) ), array ( array ( 'row' => 'HtmlTag' ), array ( 'tag' => 'tr', 'openOnly' => 'true', 'placement' => 'prepend' ) ) );
 
            $sousformElem1 = new Zend_Form_Element_Text ( 'element_1' );
            $sousformElem1->setName ( 'element_1' );
            $sousformElem1->setDescription ( 'Element 1' );
            $sousformElem1->setLabel ( 'Element 1' );
            $sousformElem1->setOrder ( 1 );
            $sousformElem1->setDecorators ( $decoratorElem1 );
            sousform2->addElement ( $sousformElem1 );
 
            // Elément 2
            $decoratorElem2 = array ('ViewHelper', array ('Description', array ('tag' => 'span', 'escape' => false) ) ), array (array ('data' => 'HtmlTag' ), array ('tag' => 'td' ) ), array ('Label', array ('tag' => 'td' ) ) );
 
            $sousformElem2 = new Zend_Form_Element_Text ( 'element_2' );
            $sousformElem2->setName ( 'element_2' );
            $sousformElem2->setDescription ( 'Elément 2' );
            $sousformElem2->setLabel( 'Elément 2' );
            $sousformElem2->setOrder ( 2 );
            $sousformElem2->setDecorators ( $decoratorElem2 );
            sousform2->addElement ( $sousformElem2 );
 
            // Elément 3
            $decoratorElem3 = array ('ViewHelper', array ('Description', array ('tag' => 'span', 'escape' => false ) ), array (array ('data' => 'HtmlTag' ), array ('tag' => 'td' ) ), array ('Label', array ('tag' => 'td' ) )  );
 
            $sousformElem3 = new Zend_Form_Element_Text ( 'element_3' );
            $sousformElem3->setName ( 'element_3' );
            $sousformElem3->setDescription ( 'Elémetn 3' );
            $sousformElem3->setLabel( 'Elément 3' );
            $sousformElem3->setOrder ( 3 );
            $sousformElem3->setDecorators ( $decoratorElem3 );
            sousform2->addElement ( $sousformElem3 );        
 
            //  Elem4
            $decoratorElem4 = array ('ViewHelper', array ('Description', array ('tag' => 'span', 'escape' => false) ), array (array ('data' => 'HtmlTag' ), array ('tag' => 'td' ) ), array ('Label', array ('tag' => 'td' ) ), array ( array ( 'row' => 'HtmlTag' ), array ( 'tag' => 'tr', 'closeOnly' => true, 'placement' => 'append' ) ) );
 
            $sousformElem4 = new Zend_Form_Element_Text ( 'element_4' );
            $sousformElem4->setName ( 'element_4' );
            $sousformElem4->setDescription ( 'Elément 4' );
            $sousformElem4->setLabel( 'Elément 4' );
            $sousformElem4->setOrder ( 4 );
            $sousformElem4->setDecorators ( $decoratorElem4 );
            sousform2->addElement ( $sousformElem4 );
 
            $sousform2->setDecorators($decorator2);
Néanmoins, je n'arrive toujours pas à afficher les intitulés
flilou est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 09/08/2011, 17h42   #5
Invité régulier
 
Homme
Développeur Web
Inscription : janvier 2009
Messages : 20
Détails du profil
Informations personnelles :
Sexe : Homme

Informations professionnelles :
Activité : Développeur Web

Informations forums :
Inscription : janvier 2009
Messages : 20
Points : 6
Points : 6
Par défaut Ajouter les header des colonnes d'un Zend_Form tableau

Bonjour,

J'ai exactement le même problème que toi, j'ai un Zend_Form rempli à partir des informations d'un tableau (exemple tiré de ce site : http://davidcaylor.com/2008/03/24/bu...-in-zend_form/) :

Code :
1
2
3
4
5
6
7
8
9
10
11
12
$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';
Code :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
$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')),
));
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 ?
niusha est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 10/08/2011, 10h13   #6
Invité régulier
 
Homme
Développeur Web
Inscription : janvier 2009
Messages : 20
Détails du profil
Informations personnelles :
Sexe : Homme

Informations professionnelles :
Activité : Développeur Web

Informations forums :
Inscription : janvier 2009
Messages : 20
Points : 6
Points : 6
Citation:
Envoyé par niusha Voir le message
Bonjour,

J'ai exactement le même problème que toi, j'ai un Zend_Form rempli à partir des informations d'un tableau (exemple tiré de ce site : http://davidcaylor.com/2008/03/24/bu...-in-zend_form/) :

Code :
1
2
3
4
5
6
7
8
9
10
11
12
$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';
Code :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
$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')),
));
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 ?
Solution à mon problème potentiellement applicable au tien (trouvé sur : http://paveldubinin.com/2011/04/real...ith-zend-form/) :

Ajouter une classe "decorator" : /library/Form/Decorator/SimpleTable.php

Code :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
 
<?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
Code :
1
2
3
4
5
6
7
 
 
$form->setDecorators(array(
    'FormElements',
    array('HtmlTag', array('tag' => 'table')),
    'Form'
));
Par :

Code :
1
2
3
4
5
6
7
8
9
 
$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'));
niusha est déconnecté   Envoyer un message privé Réponse avec citation 00
Réponse Proposer ce sujet en actualité
Outils de la discussion



Fuseau horaire GMT +2. Il est actuellement 12h59.


 
 
 
 
Partenaires

Hébergement Web