File tree Expand file tree Collapse file tree 1 file changed +17
-7
lines changed Expand file tree Collapse file tree 1 file changed +17
-7
lines changed Original file line number Diff line number Diff line change @@ -148,8 +148,17 @@ view layer:
148
148
Validation
149
149
----------
150
150
151
- Define your validation constraints outside of the forms, for instance directly on the object
152
- class used as data mapper::
151
+ The `constraints `_ option allows you to attach `validation constraints `_ to any
152
+ form field. However, doing that prevents the validation from being reused in
153
+ other forms or other places where the mapped object is used.
154
+
155
+ .. best-practice ::
156
+
157
+ Do not define your validation constraints in the form but on the object the
158
+ form is mapped to.
159
+
160
+ For example, to validate that the title of the post edited with a form is not
161
+ blank, add the following in the ``Post `` object::
153
162
154
163
// src/Entity/Post.php
155
164
@@ -158,13 +167,11 @@ class used as data mapper::
158
167
159
168
class Post
160
169
{
161
- /**
170
+ /**
162
171
* @Assert\NotBlank()
163
172
*/
164
- public $title;
165
- }
166
-
167
- This make them reusables and independents from the validation rules.
173
+ public $title;
174
+ }
168
175
169
176
Rendering the Form
170
177
------------------
@@ -228,3 +235,6 @@ like the form is *always* processed (even on the GET request).
228
235
----
229
236
230
237
Next: :doc: `/best_practices/i18n `
238
+
239
+ .. _`constraints` : https://symfony.com/doc/current/reference/forms/types/form.html#constraints
240
+ .. _`validation constraints` : https://symfony.com/doc/current/reference/constraints.html
You can’t perform that action at this time.
0 commit comments