Bonjour ,
Je voudrai avoir un formulaire avec ajout d'une marque un upload de son logo et ajout x de photo de diaporama pour celle-ci.
J'ai bien sur étais sur la doc et autre site avant de poste et voici ce que j'ai fais.
Dans un premier temps j'ai mes deux entités :
Brands :
SlideshowBrands :
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211 <?php namespace Ilan\BackBundle\Entity; use Doctrine\ORM\Mapping as ORM; use Doctrine\Common\Collections\ArrayCollection; use Symfony\Component\Validator\Constraints as Assert; use Symfony\Component\HttpFoundation\File\UploadedFile; /** * Brands * * @ORM\Table(name="brands") * @ORM\Entity * @ORM\HasLifecycleCallbacks */ class Brands { /** * @var integer * * @ORM\Column(name="brands_id", type="integer", nullable=false) * @ORM\Id * @ORM\GeneratedValue(strategy="IDENTITY") */ private $brandsId; /** * @var string * * @ORM\Column(name="brands_title", type="string", length=255, nullable=true) */ private $brandsTitle; /** * @var string * * @ORM\Column(name="brands_logo", type="string", length=255, nullable=true) * @Assert\File(maxSize="6000000") */ private $brandsLogo; /** * @var string * * @ORM\Column(name="brands_slug", type="string", length=255, nullable=true) */ private $brandsSlug; /** * @var \SlideshowBrands * @ORM\OneToMany(targetEntity="SlideshowBrands") */ private $slideshowBrands; /** * */ public function __construct() { $this->slideshowBrands = new ArrayCollection(); } /** * Get brandsId * * @return integer */ public function getBrandsId() { return $this->brandsId; } /** * Set brandsTitle * * @param string $brandsTitle * @return Brands */ public function setBrandsTitle($brandsTitle) { $this->brandsTitle = $brandsTitle; return $this; } /** * Get brandsTitle * * @return string */ public function getBrandsTitle() { return $this->brandsTitle; } /** * Set brandsLogo * * @param string $brandsLogo * @return Brands */ public function setBrandsLogo(UploadedFile $brandsLogo) { $this->brandsLogo = $brandsLogo; return $this; } /** * Get brandsLogo * * @return string */ public function getBrandsLogo() { return $this->brandsLogo; } /** * Set brandsSlug * * @param string $brandsSlug * @return Brands */ public function setBrandsSlug($brandsSlug) { $this->brandsSlug = $brandsSlug; return $this; } /** * Get brandsSlug * * @return string */ public function getBrandsSlug() { return $this->brandsSlug; } /** * Set slideshowbrands * * @param \Ilan\BackBundle\Entity\SlideshowBrands $slideshowbrands * @return */ public function setSlideshowBrands(ArrayCollection $slideshowbrands ) { $this->slideshowBrands = $slideshowbrands; return $this; } /** * Get slideshowbrands * * @return \Ilan\BackBundle\Entity\SlideshowBrands */ public function getSlideshowBrands() { return $this->slideshowBrands; } public function getUploadDir() { return 'uploads/img'; } protected function getUploadRootDir() { return __DIR__.'/../../../../web/'.$this->getUploadDir(); } /** * @ORM\PrePersist() * @ORM\PreUpdate() */ public function preUpload() { if (null === $this->brandsLogo) { return; } } /** * @ORM\PostPersist() * @ORM\PostUpdate() */ public function upload() { if (null === $this->brandsLogo) { return; } $name = str_replace(array("","é","è","à","ç"," "),array("-","e","e","a","c","-"),$this->brandsLogo->getClientOriginalName() ); $this->brandsLogo->move($this->getUploadRootDir(), $name ); } }
Mon doctrine à généré automatiquement dans SlideshowBrands le getBrands la relation ManyToOne. J'ai alors ajouter un OneToMany car j'avais une erreur comme quoi ce n'est pas une instance...
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146 <?php namespace Ilan\BackBundle\Entity; use Doctrine\ORM\Mapping as ORM; use Symfony\Component\Validator\Constraints as Assert; use Symfony\Component\HttpFoundation\File\UploadedFile; /** * SlideshowBrands * * @ORM\Table(name="slideshow_brands") * @ORM\Entity * @ORM\HasLifecycleCallbacks */ class SlideshowBrands { /** * @var integer * * @ORM\Column(name="slideshow_brands_id", type="integer", nullable=false) * @ORM\Id * @ORM\GeneratedValue(strategy="IDENTITY") */ private $slideshowBrandsId; /** * @var string * * @ORM\Column(name="slideshow_brands_picture", type="string", length=255, nullable=true) * @Assert\File(maxSize="6000000") */ private $slideshowBrandsPicture; /** * @var \Brands * * @ORM\ManyToOne(targetEntity="Brands") * @ORM\JoinColumns({ * @ORM\JoinColumn(name="brands_id", referencedColumnName="brands_id") * }) */ private $brands; /** * Get slideshowBrandsId * * @return integer */ public function getSlideshowBrandsId() { return $this->slideshowBrandsId; } /** * Set slideshowBrandsPicture * * @param string $slideshowBrandsPicture * @return SlideshowBrands */ public function setSlideshowBrandsPicture(UploadedFile $slideshowBrandsPicture) { $this->slideshowBrandsPicture = $slideshowBrandsPicture; return $this; } /** * Get slideshowBrandsPicture * * @return string */ public function getSlideshowBrandsPicture() { return $this->slideshowBrandsPicture; } /** * Set brands * * @param \Ilan\BackBundle\Entity\Brands $brands * @return SlideshowBrands */ public function setBrands(\Ilan\BackBundle\Entity\Brands $brands = null) { $this->brands = $brands; return $this; } /** * Get brands * * @return \Ilan\BackBundle\Entity\Brands */ public function getBrands() { return $this->brands; } public function getUploadDir() { return 'uploads/img'; } protected function getUploadRootDir() { return __DIR__.'/../../../../web/'.$this->getUploadDir(); } /** * @ORM\PrePersist() * @ORM\PreUpdate() */ public function preUpload() { if (null === $this->slideshowBrandsPictur) { return; } } /** * @ORM\PostPersist() * @ORM\PostUpdate() */ public function upload() { if (null === $this->slideshowBrandsPictur) { return; } $name = str_replace(array("","é","è","à","ç"," "),array("-","e","e","a","c","-"),$this->slideshowBrandsPictur->getClientOriginalName() ); $this->slideshowBrandsPictur->move($this->getUploadRootDir(), $name ); } }
Voici mes deux classes de formulaire:
BrandssAddType:
SlideshowBrandsAddType:
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 <?php namespace Ilan\BackBundle\Form\Type; use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\OptionsResolver\OptionsResolverInterface; class BrandsAddType extends AbstractType { public function buildForm(FormBuilderInterface $builder, array $options) { $builder->add('brands_title', 'text') ->add('brands_logo', 'file') ->add('slideshowbrands', 'collection', array('type' => new \Ilan\BackBundle\Form\Type\SlideshowBrandsAddType(), 'allow_add' => true, 'allow_delete' => true,)); } public function setDefaultOptions(OptionsResolverInterface $resolver) { $resolver->setDefaults(array( 'data_class' => 'Ilan\BackBundle\Entity\Brands' ) ); } public function getName() { return 'brandsAdd'; } }
Ici j'ai suivie la doc de symfony. Normalement tout est bon.
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 <?php namespace Ilan\BackBundle\Form\Type; use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\OptionsResolver\OptionsResolverInterface; class SlideshowBrandsAddType extends AbstractType { public function buildForm(FormBuilderInterface $builder, array $options) { $builder->add('slideshowBrandsPicture', 'file'); } public function setDefaultOptions(OptionsResolverInterface $resolver) { $resolver->setDefaults(array( 'data_class' => 'Ilan\BackBundle\Entity\SlideshowBrands' ) ); } public function getName() { return 'slideshowBrandsAdd'; } }
Ensuite voici mon controller:
Et la mon problème c'est que je ne rentre pas dans mon isValid. Je ne vois pas d'ou cela viens, car je rempli bien tout mon formulaire.
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 <?php namespace Ilan\BackBundle\Controller; use Symfony\Bundle\FrameworkBundle\Controller\Controller; use Ilan\BackBundle\Entity\Brands; use Ilan\BackBundle\Form\Type\BrandsAddType; use Symfony\Component\HttpFoundation\Request; class BrandsController extends Controller { public function indexAction() { return $this->render('IlanBackBundle:Brands:index.html.twig'); } public function addAction(Request $request) { $brands = new Brands(); $form = $this->createForm(new BrandsAddType(), $brands); if ($request->isMethod('POST')) { $form->bind($request); if ($form->isValid()) { $this->redirect($this->generateUrl('/admin/marques')); } } return $this->render('IlanBackBundle:Brands:add.html.twig', array( 'form' => $form->createView(), )); } }
Voici mon twig
Je ne vois pas le problème si quelqu'un peut m'aider.
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113 {% extends "::admin_layout.html.twig" %} {% block body %} {# Inclusion template Header #} {% include "IlanBackBundle:Default:header.html.twig" %} <div class="row-fluid"> <div class="span7"> <div class="box paint color_3"> <div class="title"> <h4><span>Ajout d'une marque</span> </h4> </div> <div class="content"> <form action="{{ path('ilan_back_marques_add') }}" {{ form_enctype(form) }} method="POST" class="form-horizontal row-fluid" > <div class="form-row control-group row-fluid"> <label class="control-label span3" for="normal-field">{{ form_label(form.brands_title, "Titre de la marque") }}</label> <div class="controls span9"> {{ form_widget(form.brands_title) }} </div> </div> <div class="form-row control-group row-fluid"> <label class="control-label span3" for="search-input">{{ form_label(form.brands_logo, "Ajouter un logo") }}</label> <div class="controls span9"> <div class="input-append row-fluid"> {{ form_widget(form.brands_logo) }} </div> </div> </div> <div class="form-row control-group row-fluid"> <label class="control-label span3" id="labelslideshowbrands" for="search-input">{{ form_label(form.slideshowbrands, "Ajouter une/des photos pour le diaporama") }}</label> <div class="controls span9"> <div class="input-append row-fluid"> {{ form_widget(form.slideshowbrands) }} </div> </div> </div> <div class="form-actions row-fluid"> <div class="span3 visible-desktop"></div> <div class="span7 "> <button type="submit" class="btn btn-primary">Enregister</button> </div> </div> </form> </div> </div> <!-- End .box --> </div> </div> <script src="http://code.jquery.com/jquery-1.8.2.min.js"></script> <script type="text/javascript"> $(document).ready(function() { var $container = $('div#brandsAdd_slideshowbrands'); var $lienAjout = $('<a href="#" id="ajout_marque" class="btn btn-success">Ajouter</a>'); $container.append($lienAjout); $lienAjout.click(function(e) { ajouterCategorie($container); e.preventDefault(); return false; }); var index = $container.find(':input').length; if (index == 0) { ajouterCategorie($container); } else { $container.children('div').each(function() { ajouterLienSuppression($(this)); }); } function ajouterCategorie($container) { // Dans le contenu de l'attribut « data-prototype », on remplace : // - le texte "__name__label__" qu'il contient par le label du champ // - le texte "__name__" qu'il contient par le numéro du champ var $prototype = $($container.attr('data-prototype').replace(/__name__label__/g, '') .replace(/__name__/g, index)); ajouterLienSuppression($prototype); $container.append($prototype); $("#brandsAdd_slideshowbrands_"+index+" label").html('Image'); index++; } function ajouterLienSuppression($prototype) { $lienSuppression = $('<a href="#" class="btn btn-danger">Supprimer</a>'); $prototype.append($lienSuppression); $lienSuppression.click(function(e) { $prototype.remove(); e.preventDefault(); // évite qu'un # apparaisse dans l'URL return false; }); } }); </script> {# Inclusion template Footer #} {% include "IlanBackBundle:Default:footer.html.twig" %} {% endblock %}
Merci
Partager