Bonjour à tous !
Voilà mon problème : je cherche à setter une valeur par défaut afin d'afficher ou non un champ dans mon formulaire.
J'ai deux boutons qui envoie sur le même formulaire : ajouter un ouvrage à l'échange | ajouter un ouvrage à la vente ; ce que je veux c'est que lorsque je clique sur "ajouter l'ouvrage à la vente" le champ input prix apparaisse. Pour cela j'ai créé une entité Statuts_Ouvrages avec deux sortes de libellée : libellee_statut => à échanger | libellee_statut_2 => à vendre.
Voici le code de mon entité Ouvrages :
Voici le code mon entité Statuts_Ouvrages :
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466 <?php namespace SB\MainBundle\Entity; use Doctrine\ORM\Mapping as ORM; use Symfony\Component\Validator\Constraints as Assert; use Symfony\Component\HttpFoundation\File\UploadedFile; /** * Ouvrages * * @ORM\Table(name="ouvrages") * @ORM\Entity(repositoryClass="SB\MainBundle\Repository\OuvragesRepository") * @ORM\HasLifecycleCallbacks() */ class Ouvrages { /** * @var bool * * @ORM\Column(name="is_delete", type="boolean",nullable=true) */ private $isDelete; /** * @ORM\OneToOne(targetEntity="SB\MainBundle\Entity\Editeurs") * @ORM\JoinColumn(name="editeur_id", referencedColumnName="id") */ private $editeur; /** * @ORM\ManyToOne(targetEntity="SB\MainBundle\Entity\StatutsOuvrages") * @ORM\JoinColumn(name="id_statut_ouvrage",referencedColumnName="id",onDelete="CASCADE") */ private $statut_ouvrage; /** * @ORM\ManyToOne(targetEntity="SB\MainBundle\Entity\Categories") * @ORM\JoinColumn(name="id_categorie",referencedColumnName="id",onDelete="CASCADE") */ private $categorie; /** * @ORM\ManyToMany(targetEntity="SB\MainBundle\Entity\Genres", cascade={"persist"}) */ private $genre; /** * @ORM\ManyToMany(targetEntity="SB\MainBundle\Entity\Auteurs", cascade={"persist"}) */ private $auteur; /** * @ORM\ManyToOne(targetEntity="SB\UserBundle\Entity\User") * @ORM\JoinColumn(name="id_user",referencedColumnName="id",onDelete="CASCADE") */ private $user; /** * @var int * * @ORM\Column(name="id", type="integer") * @ORM\Id * @ORM\GeneratedValue(strategy="AUTO") */ private $id; /** * @var string * * @ORM\Column(name="titre_ouvrage", type="string", length=255) */ private $titreOuvrage; /** * @var string * * @ORM\Column(name="resume_ouvrage", type="text") */ private $resumeOuvrage; /** * @var string * * @ORM\Column(name="photos_ouvrage_path", type="string", length=255) */ private $photosOuvragePath; /** * @var float * * @ORM\Column(name="prix_ouvrage", type="float") */ private $prixOuvrage; /** * @Assert\File( * maxSize = "1024k", * maxSizeMessage = "Votre fichier est trop volumineux, max 1 MO !", * mimeTypes ={"image/jpeg","image/png","image/gif"}, * mimeTypesMessage = "Merci d'uploader un fichier de type image !" * ) */ private $photosOuvrageFile; /** * Constructor */ public function __construct($user) { $this->user = $user; $this->genre = new \Doctrine\Common\Collections\ArrayCollection(); $this->auteur = new \Doctrine\Common\Collections\ArrayCollection(); } /** * Get id * * @return integer */ public function getId() { return $this->id; } /** * Set titreOuvrage * * @param string $titreOuvrage * @return Ouvrages */ public function setTitreOuvrage($titreOuvrage) { $this->titreOuvrage = $titreOuvrage; return $this; } /** * Get titreOuvrage * * @return string */ public function getTitreOuvrage() { return $this->titreOuvrage; } /** * Set resumeOuvrage * * @param string $resumeOuvrage * @return Ouvrages */ public function setResumeOuvrage($resumeOuvrage) { $this->resumeOuvrage = $resumeOuvrage; return $this; } /** * Get resumeOuvrage * * @return string */ public function getResumeOuvrage() { return $this->resumeOuvrage; } /** * Set photosOuvragePath * * @param string $photosOuvragePath * @return Ouvrages */ public function setPhotosOuvragePath($photosOuvragePath) { $this->photosOuvragePath = $photosOuvragePath; return $this; } /** * Get photosOuvragePath * * @return string */ public function getPhotosOuvragePath() { return $this->photosOuvragePath; } /** * Set categorie * * @param \SB\MainBundle\Entity\Categories $categorie * @return Ouvrages */ public function setCategorie(\SB\MainBundle\Entity\Categories $categorie = null) { $this->categorie = $categorie; return $this; } /** * Get categorie * * @return \SB\MainBundle\Entity\Categories */ public function getCategorie() { return $this->categorie; } /** * Add genre * * @param \SB\MainBundle\Entity\Genres $genre * @return Ouvrages */ public function addGenre(\SB\MainBundle\Entity\Genres $genre) { $this->genre[] = $genre; return $this; } /** * Remove genre * * @param \SB\MainBundle\Entity\Genres $genre */ public function removeGenre(\SB\MainBundle\Entity\Genres $genre) { $this->genre->removeElement($genre); } /** * Get genre * * @return \Doctrine\Common\Collections\Collection */ public function getGenre() { return $this->genre; } /** * Add auteur * * @param \SB\MainBundle\Entity\Auteurs $auteur * @return Ouvrages */ public function addAuteur(\SB\MainBundle\Entity\Auteurs $auteur) { $this->auteur[] = $auteur; return $this; } /** * Remove auteur * * @param \SB\MainBundle\Entity\Auteurs $auteur */ public function removeAuteur(\SB\MainBundle\Entity\Auteurs $auteur) { $this->auteur->removeElement($auteur); } /** * Get auteur * * @return \Doctrine\Common\Collections\Collection */ public function getAuteur() { return $this->auteur; } /** * Set user * * @param \SB\UserBundle\Entity\User $user * @return Ouvrages */ public function setUser(\SB\UserBundle\Entity\User $user = null) { $this->user = $user; return $this; } /** * Get user * * @return \SB\UserBundle\Entity\User */ public function getUser() { return $this->user; } /** * @return mixed */ public function getPhotosOuvrageFile(){ return $this->photosOuvrageFile; } /** * @param mixed $photosOuvrageFile */ public function setPhotosOuvrageFile(UploadedFile $photosOuvrageFile) { //$photosOuvrageFile devient du type UploadedFile $this->photosOuvrageFile = $photosOuvrageFile; } //retourner le chemin absolu d'un fichier public function getAbsolutePath(){ return null === $this->photosOuvragePath ? null : $this->getUploadRootDir().'/'.$this->photosOuvragePath; } //retourner le chemain relatif d'un fichier (methode utilisee dans les templates Twig) public function getWebPath(){ return null === $this->photosOuvragePath ? null : $this->getUploadDir().'/'.$this->photosOuvragePath; } protected function getUploadRootDir(){ return __DIR__.'/../../../../web/'.$this->getUploadDir(); } protected function getUploadDir(){ return "uploads/images"; } /** * @ORM\PrePersist() * @ORM\PreUpdate() */ public function preUpload(){ if (null !== $this->photosOuvrageFile){ //Avant l'uploads on change le nom du fichier téléchargé via des méthodes CRYPT $this->photosOuvragePath = sha1(uniqid(mt_rand(),true)).'.'.$this->photosOuvrageFile->guessExtension(); // donne par exemple = "55112sdfs2111qdrgthhztq412.jpg" } } /** * @ORM\PrePersist() * @ORM\PreUpdate() */ public function upload(){ //si pas d'image on stoppe la fonction if (null === $this->photosOuvrageFile){ return; } //si fichier déplacement vers le répertoire uploads $this->photosOuvrageFile->move($this->getUploadRootDir(),$this->photosOuvragePath); //puis on vide l'attribut $this->photosOuvrageFile = null; } /** * @ORM\PostRemove() */ public function removeUpload(){ if ($file = $this->getAbsolutePath()){ unlink($file); } } /** * Set statut_ouvrage * * @param \SB\MainBundle\Entity\StatutsOuvrages $statutOuvrage * @return Ouvrages */ public function setStatutOuvrage(\SB\MainBundle\Entity\StatutsOuvrages $statutOuvrage = null) { $this->statut_ouvrage = $statutOuvrage; return $this; } /** * Get statut_ouvrage * * @return \SB\MainBundle\Entity\StatutsOuvrages */ public function getStatutOuvrage() { return $this->statut_ouvrage; } /** * Set prixOuvrage * * @param float $prixOuvrage * @return Ouvrages */ public function setPrixOuvrage($prixOuvrage) { $this->prixOuvrage = $prixOuvrage; return $this; } /** * Get prixOuvrage * * @return float */ public function getPrixOuvrage() { return $this->prixOuvrage; } /** * Set isDelete * * @param boolean $isDelete * @return Ouvrages */ public function setIsDelete($isDelete) { $this->isDelete = $isDelete; return $this; } /** * Get isDelete * * @return boolean */ public function getIsDelete() { return $this->isDelete; } /** * Set editeur * * @param \SB\MainBundle\Entity\Editeurs $editeur * @return Ouvrages */ public function setEditeur(\SB\MainBundle\Entity\Editeurs $editeur = null) { $this->editeur = $editeur; return $this; } /** * Get editeur * * @return \SB\MainBundle\Entity\Editeurs */ public function getEditeur() { return $this->editeur; } }
Voici le code de mon controller dans lequel je veux setter la valeur par défaut :
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 <?php namespace SB\MainBundle\Entity; use Doctrine\ORM\Mapping as ORM; /** * StatutsOuvrages * * @ORM\Table(name="statuts_ouvrages") * @ORM\Entity(repositoryClass="SB\MainBundle\Repository\StatutsOuvragesRepository") */ class StatutsOuvrages { /** * @var int * * @ORM\Column(name="id", type="integer") * @ORM\Id * @ORM\GeneratedValue(strategy="AUTO") */ private $id; /** * @var string * * @ORM\Column(name="libelle_statut", type="string", length=255) */ private $libelleStatut; /** * @var string * * @ORM\Column(name="libelle_statut2", type="string", length=255) */ private $libelleStatut2; /** * Get id * * @return integer */ public function getId() { return $this->id; } /** * Set libelleStatut * * @param string $libelleStatut * @return StatutsOuvrages */ public function setLibelleStatut($libelleStatut) { $this->libelleStatut = $libelleStatut; return $this; } /** * Get libelleStatut * * @return string */ public function getLibelleStatut() { return $this->libelleStatut; } /** * Set libelleStatut2 * * @param string $libelleStatut2 * @return StatutsOuvrages */ public function setLibelleStatut2($libelleStatut2) { $this->libelleStatut2 = $libelleStatut2; return $this; } /** * Get libelleStatut2 * * @return string */ public function getLibelleStatut2() { return $this->libelleStatut2; } }
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 public function createNewOuvrageAction(Request $request){ $user = $this->get('security.token_storage')->getToken()->getUser(); $ouvrage = new Ouvrages($user); $em = $this->getDoctrine()->getManager(); $form = $this->createForm(OuvragesType::class,$ouvrage); $formHandler = new FormHandler($request,$em,$form); //si validation du formulaire et données >> ok ! if ($formHandler->process()){ return $this->redirect($this->generateUrl('sb_main_ouvrage_echange_show',['id'=>$ouvrage->getId()])); } if ($form->isSubmitted() && $form->isValid()) { // $file stores the uploaded img file /** @var Symfony\Component\HttpFoundation\File\UploadedFile $file */ $file = $ouvrage->getPhotosOuvrageFile(); // Generate a unique name for the file before saving it $fileName = md5(uniqid()).'.'.$file->guessExtension(); // Move the file to the directory where brochures are stored $file->move($this->getParameter('brochures_directory'), $fileName); // Update the 'ouvrage' property to store the img file name // instead of its contents $ouvrage->setPhotosOuvrageFile($fileName); // ... persist the $ouvrage variable or any other work return $this->redirect($this->generateUrl('sb_main_ouvrage_show',['id'=>$ouvrage->getId()])); } $datas = array('form'=>$form->createView()); return $this->render('SBMainBundle:Main:createOuvrage.html.twig',$datas); }
Partager