Skip to content

Commit 68a7931

Browse files
committed
Reword
1 parent 21c4c9f commit 68a7931

File tree

1 file changed

+17
-7
lines changed

1 file changed

+17
-7
lines changed

best_practices/forms.rst

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -148,8 +148,17 @@ view layer:
148148
Validation
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

169176
Rendering the Form
170177
------------------
@@ -228,3 +235,6 @@ like the form is *always* processed (even on the GET request).
228235
----
229236

230237
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

0 commit comments

Comments
 (0)