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:
148148Validation
149149----------
150150
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::
153162
154163 // src/Entity/Post.php
155164
@@ -158,13 +167,11 @@ class used as data mapper::
158167
159168 class Post
160169 {
161- /**
170+ /**
162171 * @Assert\NotBlank()
163172 */
164- public $title;
165- }
166-
167- This make them reusables and independents from the validation rules.
173+ public $title;
174+ }
168175
169176Rendering the Form
170177------------------
@@ -228,3 +235,6 @@ like the form is *always* processed (even on the GET request).
228235----
229236
230237Next: :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