Skip to content

Improved Form Validation Docs #2058

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Dec 30, 2012
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions book/forms.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1581,6 +1581,39 @@ an array of data, instead of an object. In most cases, it's better - and
certainly more robust - to bind your form to an object. But for simple forms,
this is a great approach.

.. versionadded:: 2.1
It is now possible to add constraints directly to the fields using the
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is misleading. validation_constraint was also applying on the field directly. The difference is that it was impossible to add several constraints

``constraints`` option, which accepts a single constraint or an array
of them.

.. code-block:: php

use Symfony\Component\Validator\Constraints\MinLength;
use Symfony\Component\Validator\Constraints\NotBlank;

$builder
->add('firstName', 'text', array(
'constraints' => new MinLength(3),
))
->add('lastName', 'text', array(
'constraints' => array(
new NotBlank(),
new MinLength(3),
),
))
;

.. tip::

If you are using Validation Groups, you need to either reference the
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should put an empty line before this line

``Default`` group when creating the form, or set the correct group on
the constraint you are adding.

.. code-block:: php

new NotBlank(array('groups' => array('create', 'update'))


Final Thoughts
--------------

Expand Down