Skip to content

Add an explanation about «constraints» validation #7664

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

Closed
wants to merge 2 commits into from
Closed
Changes from 1 commit
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
Prev Previous commit
Rewords and minor fixes
  • Loading branch information
javiereguiluz authored Mar 22, 2017
commit 6d2a1e41100431f3a7570d3d53ff68e736fd10c7
32 changes: 19 additions & 13 deletions validation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -449,27 +449,33 @@ If you're ever unsure of how to specify an option, either check the API document
for the constraint or play it safe by always passing in an array of options
(the first method shown above).

.. index::
single: Validation; Constraint targets

.. _validator-constraint-targets:

Constraint in Form Classes
------------------
Constraints in Form Classes
---------------------------

When creating your own form classes, you may want to add constraint directly in the formBuilder.
This is done by simply adding them as a parameter in your field options::
Constraints can be defined while building the form via the ``constraints`` option
of the form fields::

public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('myField', TextType::class, ['required' => true, 'constraints' => [new Length(['min' => 3])]])
->add('myField', TextType::class, array(
'required' => true,
'constraints' => array(new Length(array('min' => 3)))
))
}

Please note the *constraints* keyword will only be available if you have
added the *ValidatorExtention* to the formBuilder::
The ``constraints`` option is only available when adding the ValidatorExtention
to the formBuilder::

Forms::createFormFactoryBuilder()->addExtension(new ValidatorExtension(Validation::createValidator()))->getFormFactory();
Forms::createFormFactoryBuilder()
->addExtension(new ValidatorExtension(Validation::createValidator()))
->getFormFactory()
;

.. index::
single: Validation; Constraint targets

.. _validator-constraint-targets:

Constraint Targets
------------------
Expand Down