Bonjour à tous,
j'aimerai savoir s'il est de manière rapide et facile de faire une validation d'un champ numérique dans un formulaire, j'ai cherché dans la doc et le forum mais à part cette discussion de 2015, je n'ai rien trouvé :
https://www.developpez.net/forums/d1...-type-integer/
Voici mon code, j'ai le problème pour le champ cp_client (entre autre) quand je saisis de l'alphanumérique, j'ai ce message d'erreur : Expected argument of type "int", "string" given at property path "cp_client".
Entité Client
Le form CreerClientType
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 <?php namespace App\Entity; use App\Repository\ClientRepository; use Doctrine\Common\Collections\ArrayCollection; use Doctrine\Common\Collections\Collection; use Doctrine\ORM\Mapping as ORM; use Symfony\Component\Validator\Constraints as Assert; /** * @ORM\Entity(repositoryClass=ClientRepository::class) */ class Client { /** * @ORM\Id * @ORM\GeneratedValue * @ORM\Column(type="integer") */ private $id; /** * @Assert\Length( * min = 2, * max = 255, * minMessage = "Le nom doit être plus long que {{ limit }} caractères", * maxMessage = "Le nom ne doit pas être plus long que {{ limit }} caractères" * ) * @ORM\Column(type="string", length=255) */ private $nom_client; /** * @Assert\Length( * min = 2, * max = 255, * minMessage = "Le prénom doit être plus long que {{ limit }} caractères", * maxMessage = "Le prénom ne doit pas être plus long que {{ limit }} caractères" * ) * @ORM\Column(type="string", length=255) */ private $prenom_client; /** * @Assert\Length( * min = 2, * max = 255, * minMessage = "L'adresse' doit être plus longue que {{ limit }} caractères", * maxMessage = "L'adresse' ne doit pas être plus longue que {{ limit }} caractères" * ) * @ORM\Column(type="string", length=255, nullable=true) */ private $adresse1_client; /** * @Assert\Length( * max = 255, * maxMessage = "Le complément d'adresse ne doit pas être plus long que {{ limit }} caractères" * ) * @ORM\Column(type="string", length=255, nullable=true) */ private $adresse2_client; /** * @Assert\Type( * type="integer", * message="Le code postal '{{ value }}' n'est pas un numérique." * ) * @Assert\Length( * min = 4, * max = 5, * minMessage = "Le code postal doit être plus long que {{ limit }} caractères", * maxMessage = "Le code postal ne doit pas être plus long que {{ limit }} caractères" * ) * @ORM\Column(type="integer") */ private $cp_client; /** * @Assert\Length( * min = 2, * max = 255, * minMessage = "La ville doit être plus longue que {{ limit }} caractères", * maxMessage = "La ville ne doit pas être plus longue que {{ limit }} caractères" * ) * @ORM\Column(type="string", length=255) */ private $ville_client; /** * @ORM\Column(type="string", length=255) */ private $pays_client; /** * @Assert\Length( * min = 2, * max = 255, * minMessage = "Le téléphone doit être plus long que {{ limit }} caractères", * maxMessage = "Le téléphone ne doit pas être plus long que {{ limit }} caractères" * ) * @ORM\Column(type="string", length=255) */ private $telephone_client; /** * @Assert\Email( * message = "L'e-mail '{{ value }}' n'est pas valide." * ) * @ORM\Column(type="string", length=255, nullable=true) */ private $mail_client; /** * @ORM\ManyToMany(targetEntity=Dossier::class, mappedBy="clients") */ private $dossiers; /** * @ORM\ManyToMany(targetEntity=Document::class, mappedBy="client") */ private $documents; /** * @ORM\Column(type="date", nullable=true, options={"default":NULL}) * @Assert\Type("\DateTimeInterface") */ private $dateNaissance_client; //* @Assert\Date() remplacé par @Assert\Type("\DateTimeInterface") /** * @ORM\Column(type="string", length=255, nullable=true) */ private $num_reg_client; /** * @ORM\Column(type="string", length=255, nullable=true) */ private $lieu_naissance_client; /** * @ORM\Column(type="string", length=255, nullable=true) */ private $nationalite_client; /** * @Assert\Type( * type="integer", * message="Le numéro '{{ value }}' n'est pas un numérique." * ) * * @ORM\Column(type="integer", nullable=true) */ private $num_adresse_client; /** * @Assert\Length( * max = 4, * maxMessage = "La boîte ne doit pas être plus long que {{ limit }} caractères" * ) * @ORM\Column(type="string", length=4, nullable=true) */ private $num_boite_adresse_client; public function __construct() { $this->dossiers = new ArrayCollection(); $this->documents = new ArrayCollection(); } public function getId(): ?int { return $this->id; } public function getNomClient(): ?string { return $this->nom_client; } public function setNomClient(string $nom_client): self { $this->nom_client = $nom_client; return $this; } public function getPrenomClient(): ?string { return $this->prenom_client; } public function setPrenomClient(string $prenom_client): self { $this->prenom_client = $prenom_client; return $this; } public function getClient(): ?string { return $this->getNomClient().' '.$this->getPrenomClient(); } public function getAdresse1Client(): ?string { return $this->adresse1_client; } public function setAdresse1Client(?string $adresse1_client): self { $this->adresse1_client = $adresse1_client; return $this; } public function getAdresse2Client(): ?string { return $this->adresse2_client; } public function setAdresse2Client(?string $adresse2_client): self { $this->adresse2_client = $adresse2_client; return $this; } public function getCPClient(): ?int { return $this->cp_client; } public function setCPClient(int $cp_client): self { $this->cp_client = $cp_client; return $this; } public function getVilleClient(): ?string { return $this->ville_client; } public function setVilleClient(string $ville_client): self { $this->ville_client = $ville_client; return $this; } public function getPaysClient(): ?string { return $this->pays_client; } public function setPaysClient(string $pays_client): self { $this->pays_client = $pays_client; return $this; } public function getTelephoneClient(): ?string { return $this->telephone_client; } public function setTelephoneClient(string $telephone_client): self { $this->telephone_client = $telephone_client; return $this; } public function getMailClient(): ?string { return $this->mail_client; } public function setMailClient(string $mail_client): self { $this->mail_client = $mail_client; return $this; } /** * @return Collection|Dossier[] */ public function getDossiers(): Collection { return $this->dossiers; } public function addDossier(Dossier $dossier): self { if (!$this->dossiers->contains($dossier)) { $this->dossiers[] = $dossier; $dossier->addClient($this); } return $this; } public function removeDossier(Dossier $dossier): self { if ($this->dossiers->removeElement($dossier)) { $dossier->removeClient($this); } return $this; } /** * @return Collection|Document[] */ public function getDocuments(): Collection { return $this->documents; } public function addDocument(Document $document): self { if (!$this->documents->contains($document)) { $this->documents[] = $document; $document->addClient($this); } return $this; } public function removeDocument(Document $document): self { if ($this->documents->removeElement($document)) { $document->removeClient($this); } return $this; } public function __toString(): String { return $this->getNomClient().' '.$this->getPrenomClient().' ('.$this->getTelephoneClient().')'; } public function getDateNaissanceClient(): ?\DateTimeInterface { return $this->dateNaissance_client; } public function setDateNaissanceClient(\DateTimeInterface $dateNaissance_client): self { $this->dateNaissance_client = $dateNaissance_client; return $this; } public function getNumRegClient(): ?string { return $this->num_reg_client; } public function setNumRegClient(?string $num_reg_client): self { $this->num_reg_client = $num_reg_client; return $this; } public function getLieuNaissanceClient(): ?string { return $this->lieu_naissance_client; } public function setLieuNaissanceClient(?string $lieu_naissance_client): self { $this->lieu_naissance_client = $lieu_naissance_client; return $this; } public function getNationaliteClient(): ?string { return $this->nationalite_client; } public function setNationaliteClient(?string $nationalite_client): self { $this->nationalite_client = $nationalite_client; return $this; } public function getNumAdresseClient(): ?int { return $this->num_adresse_client; } public function setNumAdresseClient(?int $num_adresse_client): self { $this->num_adresse_client = $num_adresse_client; return $this; } public function getNumBoiteAdresseClient(): ?string { return $this->num_boite_adresse_client; } public function setNumBoiteAdresseClient(?string $num_boite_adresse_client): self { $this->num_boite_adresse_client = $num_boite_adresse_client; return $this; } }
Mon Controller
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 <?php namespace App\Form; use App\Entity\Client; use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\Extension\Core\Type\TextType; use Symfony\Component\Form\Extension\Core\Type\NumberType; use Symfony\Component\Form\Extension\Core\Type\EmailType; use Symfony\Component\Form\Extension\Core\Type\TelType; use Symfony\Component\Form\Extension\Core\Type\CountryType; use Symfony\Component\Form\Extension\Core\Type\DateType; use Symfony\Component\Form\Extension\Core\Type\SubmitType; use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\OptionsResolver\OptionsResolver; class CreerClientType extends AbstractType { public function buildForm(FormBuilderInterface $builder, array $options): void { $builder ->setMethod('POST') ->add('nom_client', TextType::class, [ 'required' => true, 'label' => 'Nom*']) ->add('prenom_client', TextType::class, [ 'required' => true, 'label' => 'Prénom*']) ->add('date_naissance_client', DateType::class, [ 'html5' => false, 'widget' => 'single_text', 'format' => 'dd/MM/yyyy', 'disabled' => false, 'label' => 'Date de naissance (jj/mm/AAAA)*',]) ->add('num_reg_client', TextType::class, [ 'disabled' => false, 'required' => false, 'label' => 'Numéro de registre national']) ->add('lieu_naissance_client', TextType::class, [ 'disabled' => false, 'required' => false, 'label' => 'Lieu de naissance']) ->add('adresse1_client', TextType::class, [ 'required' => true, 'label' => 'Adresse*']) ->add('num_adresse_client', TextType::class, [ 'required' => false, 'invalid_message' => 'Le numéro n est pas valide!', 'label' => 'Numéro']) ->add('num_boite_adresse_client', NumberType::class, [ 'required' => false, 'label' => 'Boîte']) ->add('adresse2_client', TextType::class, [ 'required' => false, 'empty_data' => null, 'label' => 'Adresse complémentaire']) ->add('cp_client', TextType::class, [ 'required' => true, 'invalid_message' => 'Le code postal n est pas valide!', 'label' => 'Code postal*']) ->add('ville_client', TextType::class, [ 'required' => true, 'label' => 'Ville*']) ->add('pays_client', CountryType::class, [ 'preferred_choices' => ['BE'], 'choice_translation_domain' => 'fr', 'choice_translation_locale' => 'fr', 'label' => 'Pays*', ]) ->add('telephone_client', TelType::class, [ 'required' => true, 'label' => 'Téléphone*']) ->add('mail_client', EmailType::class, [ 'required' => false, 'empty_data' => null, 'label' => 'E-Mail']) ->add('enregistrer', SubmitType::class, ['label' => 'Enregistrer' ]) ->add('creer_client', SubmitType::class, ['label' => 'Créer autre client']) ->add('creer_dossier', SubmitType::class, ['label' => 'Créer dossier']); } public function configureOptions(OptionsResolver $resolver): void { $resolver->setDefaults([ 'data_class' => Client::class, ]); } }
Mon template
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 <?php namespace App\Controller; use App\Entity\Client; use App\Form\CreerClientType; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Routing\Annotation\Route; use Doctrine\Persistence\ManagerRegistry; class CreerClientController extends AbstractController { /** * @Route("/creer/client", name="creer_client") */ public function index(Request $request, ManagerRegistry $doctrine): Response { $client = new Client(); $form = $this->createForm(CreerClientType::class, $client); // On récupère la requête du formulaire $form->handleRequest($request); if ($form->isSubmitted() && $form->isValid()) { $em = $doctrine->getManager(); /* -------------------------------------------- --------- Enregistrement du client -------- -------------------------------------------- */ // Enregistrement en base de données $em->persist($client); $em->flush(); // Redirection selon choix de l'utilisateur (créer le client -> mes dossiers // créer autre client -> creer client // nouveau_dossier -> créer dossier) $nextAction = $form->get('enregistrer')->isClicked() ? 'mes_dossiers' : ( $form->get('creer_client')->isClicked() ? 'creer_client' : 'creer_dossier_client' ); if ($nextAction = 'creer_dossier_client') { return $this->redirectToRoute('creer_dossier_client', ['id' => $client->getId()]); } else { return $this->redirectToRoute($nextAction); } } return $this->render('creer_client/index.html.twig', [ 'form_creerClient' => $form->createView(), ]); } }
Code twig : 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 {% extends 'base.html.twig' %} {% block title %}Nouveau client{% endblock %} {% block body %} <fieldset> <div class= "container-fluid"> <div class=row> <div class="col-md-6"> <p> </p> </div> </div> <div class=row> <div class="col-md-6"> <h4><span class="badge bg-info">Créer un nouveau client</span></h4> </div> </div> {{ form_start(form_creerClient)}} <div class="row"> <div class="col-md-6"> <p> </p> </div> </div> <div class="row"> <div class="col-md-6"> <span class="badge bg-info" data-bs-toggle="tooltip" data-bs-placement="left" title="Champs obligatoires">* : champs obligatoires.</span> </div> </div> <div class="row"> <div class="col-md-6"> <p> </p> </div> </div> <div class="row"> <div class="col-md-3"> {{ form_errors(form_creerClient.nom_client) }} {{ form_row(form_creerClient.nom_client) }} </div> <div class="col-md-3"> {{ form_errors(form_creerClient.prenom_client) }} {{ form_row(form_creerClient.prenom_client) }} </div> </div> <div class=row> <div class="col-md-3"> {{ form_errors(form_creerClient.num_reg_client) }} {{ form_row(form_creerClient.num_reg_client) }} </div> <div class="col-md-3"> {{ form_errors(form_creerClient.lieu_naissance_client) }} {{ form_row(form_creerClient.lieu_naissance_client) }} </div> </div> <div class=row> <div class="col-md-6"> <p> </p> </div> </div> <div class="col-md-3"> {{ form_errors(form_creerClient.date_naissance_client) }} {{ form_row(form_creerClient.date_naissance_client) }} </div> <div class=row> <div class="col-md-6"> <p> </p> </div> <div class="row"> <div class="col-md-3"> {{ form_errors(form_creerClient.adresse1_client) }} {{ form_row(form_creerClient.adresse1_client) }} </div> <div class="col-md-2"> {{ form_errors(form_creerClient.num_adresse_client) }} {{ form_row(form_creerClient.num_adresse_client) }} </div> <div class="col-md-1"> {{ form_errors(form_creerClient.num_boite_adresse_client) }} {{ form_row(form_creerClient.num_boite_adresse_client) }} </div> </div> <div class="row"> <div class="col-md-6"> {{ form_errors(form_creerClient.adresse2_client) }} {{ form_row(form_creerClient.adresse2_client) }} </div> </div> <div class="row"> <div class="col-md-2"> {{ form_errors(form_creerClient.cp_client) }} {{ form_row(form_creerClient.cp_client) }} </div> <div class="col-md-2"> {{ form_errors(form_creerClient.ville_client) }} {{ form_row(form_creerClient.ville_client) }} </div> <div class="col-md-2"> {{ form_errors(form_creerClient.pays_client) }} {{ form_row(form_creerClient.pays_client) }} </div> </div> <div class="row"> <div class="col-md-3"> {{ form_errors(form_creerClient.telephone_client) }} {{ form_row(form_creerClient.telephone_client) }} </div> <div class="col-md-3"> {{ form_errors(form_creerClient.mail_client) }} {{ form_row(form_creerClient.mail_client) }} </div> </div> <div class="row"> <div class="col-md-2" align="left"> <p>{{ form_row(form_creerClient.enregistrer) }}</p> </div> <div class="col-md-2" align="center"> <p>{{ form_row(form_creerClient.creer_client) }}</p> </div> <div class="col-md-2" align="right"> <p>{{ form_row(form_creerClient.creer_dossier) }}</p> </div> </div> {{ form_row(form_creerClient._token)}} <!--{{ form_rest(form_creerClient)}}--> {{ form_end(form_creerClient)}} </div> </fieldset> {% endblock %}
Merci d'avance et bon week-end.
Partager