Bonjour,
je galère pour imbriquer dynamiquement des forms sur symfony et j'aimerai vos avis et conseils. J'ai cherché un peu partout, j'ai trouvé des bouts de réponse mais qui ne correspondent pas exactement à mon cas. J'ai donc des articles que j'aimerai associer à des tags ou non.
voici mon schéma (je simplifie au maximum):
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
 
Tag:
  connection: doctrine
  tableName: tag
  columns:
    id:
      type: integer(8)
      fixed: false
      unsigned: false
      primary: true
      sequence: tag_id
    nom:
      type: string()
      fixed: false
      unsigned: false
      notnull: true
      primary: false
  relations:
    ArticleTag:
      local: id
      foreign: tag_id
      type: many
Article:
  connection: doctrine
  tableName: article
  columns:
    id:
      type: integer(4)
      fixed: false
      unsigned: false
      primary: true
      sequence: article_id
    nom:
      type: string()
      fixed: false
      unsigned: false
      notnull: true
      primary: false
    choixtag:
      type: integer(2)
      fixed: false
      unsigned: false
      primary: false
      notnull: true
  relations:
    ArticleTag:
      local: id
      foreign: article_id
      type: many
ArticleTag:
  connection: doctrine
  tableName: article_tag
  columns:
    id:
      type: integer(8)
      fixed: false
      unsigned: false
      primary: true
      sequence: article_tag_id
    article_id:
      type: integer(4)
      fixed: false
      unsigned: false
      notnull: false
      primary: false
    tag_id:
      type: integer(4)
      fixed: false
      unsigned: false
      notnull: false
      primary: false
  relations:
    Article:
      local: article_id
      foreign: id
      type: one
    Tag:
      local: tag_id
      foreign: id
      type: one
j'ai ensuite créé un module Article puis j'ai modifié ma classe ArticleForm de la sorte:

Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
 
class ArticleForm extends BaseArticleForm
{
	public function configure ()
  	{  		
		$this -> setWidgets (array (
		    'nom'   => new sfWidgetFormInput (),
                    'choixtag'   => new sfWidgetFormDoctrineChoice (0 => "non",  1 => "oui")
	        ));
 
	       $this -> widgetSchema -> setLabels (
                    blabla...
		));
 
		$this -> setValidators (
                    blabla...
		));
 
		$this -> validatorSchema -> setPostValidator (
                    blabla...
		);
 
		$this -> widgetSchema -> setNameFormat ('article[%s]');
 
  	}
}
puis j'ai modifié mon template newSuccess.php de mon module Article:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
 
<?php use_javascript('/sfFormExtraPlugin/js/double_list.js') ?>
 
<script type="text/javascript">
jQuery().ready(function() {
 
	var choixId = jQuery("#article_article_tag_id").val();
	articletagadd (choixId);
 
	jQuery("#article_article_tag_id").change(
		function(event) {
			var choixId = jQuery(event.target).val();
			articletagadd (choixId);
  		}
  	);
});
 
function articletagadd (choixId) {
	if (1 == choixId) {
		jQuery.post('<?php echo url_for('articletag/add') ?>',
		{},
		function(data)
		{			    	
			jQuery("#affectation").html(data);
		});
	} else {
		jQuery("#affectation").empty();
	}
}
 
</script>
 
<?php use_helper ('I18N') ?>
 
<form action="<?php echo url_for('article/create') ?>" method="post" <?php $form -> isMultipart() and print 'enctype="multipart/form-data" ' ?> id="newArticle" class="form">
 
		<?php if ($form -> hasGlobalErrors ()): ?>
	    	<ul class="globalErrors">
	    		<?php foreach ($form -> getGlobalErrors () as $name => $error): ?>
	        	<li>- <?php echo $error ?></li>
	        	<?php endforeach; ?>
	    	</ul>
		<?php endif; ?>	
 
		<fieldset>
 
			<?php foreach ($form as $widget): ?>
 
				<?php if (!$widget -> isHidden ()) { ?>
 
				<div class="widget">
 
					<?php if ($widget -> hasError ()): ?>
					<div class="error">
					<?php echo $widget -> renderError () ?>
					</div>
					<?php endif; ?>
 
					<?php echo $widget -> renderLabel () ?>
 
					<?php echo $widget -> render() ?>
 
					<?php if ($widget -> renderHelp ()): ?>
					<p class="note"><?php echo $widget -> renderHelp () ?></p>
					<?php endif; ?>
 
				</div>
 
				<?php } else { ?>
 
					<?php echo $widget -> render () ?>
 
				<?php } ?>
 
			<?php endforeach; ?>
 
			<div id="affectation" />
 
		</fieldset>
 
		<button type="submit" class="button"><?php echo __('Add') ?></button>
 
</form>
j'ai aussi créé mon module ArticleTag pour l'association donc voici la classe ArticleTagForm:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
 
class ArticleTagForm extends BaseArticleTagForm
{
	public function configure()
  	{
  		$aChoices = array();
		foreach ($aTags = Doctrine::getTable ('Tag') -> getAll() as $oTag) {
  			$aChoices[$oTag-> id] = $oTag-> nom;
		}
 
  		$this -> setWidgets (array (
		     'affectation' => new sfWidgetFormSelectDoubleList (array(
	    	 	'choices' => $aChoices,
	    	  	'label_associated' => 'Associated',  
	    	  	'label_unassociated' => 'Unassociated'
	    	 ))
	    ));
 
	    $this -> widgetSchema -> setLabels (array (
	  		'affectation' => 'Assignment'
		));
 
  		$this -> widgetSchema -> setNameFormat ('article[%s]');
  	}
}
mon action:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
 
public function executeAdd (sfWebRequest $request)
  {
    $this->form = new ArticleTagForm ();
  }
et mon template addSuccess:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
 
<?php use_helper ('I18N') ?>
 
<div class="widget">
 
		<?php if ($form['affectation'] -> hasError ()): ?>
		<div class="error">
		<?php echo $form['affectation'] -> renderError () ?>
		</div>
		<?php endif; ?>
 
		<?php echo $form['affectation'] -> renderLabel () ?>
 
		<?php echo $form['affectation'] -> render() ?>
 
</div>
donc c'est tout beau j'ai bien mon formulaire, quant je sélectionne le choix "oui" j'ai bien mon multiplemachinselect qui s'affiche avec mes tag dans "non associé".
Cependant quant je clique sur le bouton "ajouter" j'ai ce message : "Unexpected extra form field named "affectation"."
je me doute bien qu'il faut lui dire quelque part qu'il y a un champ affection à associer a mon formulaire principale. De plus je perd mes choix de sélection de tag (faut imaginer que j'en ai sélectionné bcp ^^). Apres ou est qu'on rajoute en DB l'association Article Form ? du coté du formulaire principale ? ou de actionclass.php du module ArticleTag.
je suis un peu perdu si vous avez des idées, si j'ai fait fausse route ou pas, des exemples d'utilisation, je suis preneur.
Merci d'avance
Wind