Bonjour,
J'ai un formulaire qui permet d'enregistrer un nouvel utilisateur dans la BDD. Par contre, le formulaire n'est pas à jour comme le code.
Normalement, lorsque le pseudo n'est pas rempli cela doit me mettre "Please enter a pseudo". Or cela me met "Please fill out this field".
Pour les messages d'erreur, cela fonctionne.
J'ai une question pour le dateOfBirth, je ne peux choisir qu'entre 2014 et 2024 pour l'année. Comment faire pour avoir plus de possibilités?
De même, j'aimerais personnaliser les noms des champs mais je n'y arrive pas. J'ai seulement réussi à personnaliser le bouton "register" dans register.html.twig. Comment faire du coup?
Je vous remercie par avance,
Voilà mon code:
RegistrationFormType.php
register.html.twig
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 <?php namespace App\Form; use App\Entity\User; use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\Extension\Core\Type\CheckboxType; use Symfony\Component\Form\Extension\Core\Type\DateType; use Symfony\Component\Form\Extension\Core\Type\EmailType; use Symfony\Component\Form\Extension\Core\Type\PasswordType; use Symfony\Component\Form\Extension\Core\Type\TextareaType; use Symfony\Component\Form\Extension\Core\Type\TextType; use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\OptionsResolver\OptionsResolver; use Symfony\Component\Validator\Constraints\IsTrue; use Symfony\Component\Validator\Constraints\Length; use Symfony\Component\Validator\Constraints\NotBlank; class RegistrationFormType extends AbstractType { public function buildForm(FormBuilderInterface $builder, array $options) { $builder ->add('pseudo', TextType::class, [ 'constraints' => [ new NotBlank([ 'message' => 'Please enter a pseudo', ]), ], ]) ->add('dateOfBirth', DateType::class) ->add('email', EmailType::class) ->add('plainPassword', PasswordType::class, [ // instead of being set onto the object directly, // this is read and encoded in the controller 'mapped' => false, 'constraints' => [ new NotBlank([ 'message' => 'Please enter a password', ]), new Length([ 'min' => 6, 'minMessage' => 'Your password should be at least {{ limit }} characters', // max length allowed by Symfony for security reasons 'max' => 4096, ]), ], ]) ->add('description', TextareaType::class, [ 'constraints' => [ new NotBlank([ 'message' => 'You must add a description about you!', ]), new Length([ 'min' => 100, 'minMessage' => 'Please put more about you.', ]), ], ]) ->add('agreeTerms', CheckboxType::class, [ 'mapped' => false, 'constraints' => [ new IsTrue([ 'message' => 'You should agree to our terms: \n 1) You must not use inapropriate uploaded pictures. \n 2) You must be respectfull with all the users of the website. \n 3) It is a good think if you email us and give us ideas for the website', ]), ], ]) ; } public function configureOptions(OptionsResolver $resolver) { $resolver->setDefaults([ 'data_class' => User::class, ]); } }
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 {% extends 'base.html.twig' %} {% block title %}Register{% endblock %} {% block body %} {{ parent() }} <div class="container"> <h1>Register</h1> {{ form_start(registrationForm) }} {{ form_row(registrationForm.pseudo) }} {{ form_row(registrationForm.dateOfBirth) }} {{ form_row(registrationForm.email) }} {{ form_row(registrationForm.plainPassword) }} {{ form_row(registrationForm.description) }} {{ form_row(registrationForm.agreeTerms) }} <button class="btn btn-primary">Register</button> {{ form_end(registrationForm) }} </div> {% endblock %}
User.php
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 <?php namespace App\Entity; use Doctrine\ORM\Mapping as ORM; use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity; use Symfony\Component\Security\Core\User\UserInterface; /** * @ORM\Entity(repositoryClass="App\Repository\UserRepository") * @UniqueEntity(fields={"email"}, message="There is already an account with this email") */ class User implements UserInterface { /** * @ORM\Id() * @ORM\GeneratedValue() * @ORM\Column(type="integer") */ private $id; /** * @ORM\Column(type="string", length=180, unique=true) */ private $email; /** * @ORM\Column(type="json") */ private $roles = []; /** * @var string The hashed password * @ORM\Column(type="string") */ private $password; /** * @ORM\Column(type="string", length=255) */ private $pseudo; /** * @ORM\Column(type="text") */ private $description; /** * @ORM\Column(type="date") */ private $dateOfBirth; public function getId(): ?int { return $this->id; } public function getEmail(): ?string { return $this->email; } public function setEmail(string $email): self { $this->email = $email; return $this; } /** * A visual identifier that represents this user. * * @see UserInterface */ public function getUsername(): string { return (string) $this->email; } /** * @see UserInterface */ public function getRoles(): array { $roles = $this->roles; // guarantee every user at least has ROLE_USER $roles[] = 'ROLE_USER'; return array_unique($roles); } public function setRoles(array $roles): self { $this->roles = $roles; return $this; } /** * @see UserInterface */ public function getPassword(): string { return (string) $this->password; } public function setPassword(string $password): self { $this->password = $password; return $this; } /** * @see UserInterface */ public function getSalt() { // not needed when using the "bcrypt" algorithm in security.yaml } /** * @see UserInterface */ public function eraseCredentials() { // If you store any temporary, sensitive data on the user, clear it here // $this->plainPassword = null; } public function getPseudo(): ?string { return $this->pseudo; } public function setPseudo(string $pseudo): self { $this->pseudo = $pseudo; return $this; } public function getDescription(): ?string { return $this->description; } public function setDescription(string $description): self { $this->description = $description; return $this; } public function getDateOfBirth(): ?\DateTimeInterface { return $this->dateOfBirth; } public function setDateOfBirth(\DateTimeInterface $dateOfBirth): self { $this->dateOfBirth = $dateOfBirth; return $this; } }
Partager