Skip to content

Updated information about handling validation of embedded forms to Valid... #4348

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 1 commit into from
Closed
Show file tree
Hide file tree
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
20 changes: 10 additions & 10 deletions book/forms.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1234,6 +1234,8 @@ objects. For example, a registration form may contain data belonging to
a ``User`` object as well as many ``Address`` objects. Fortunately, this
is easy and natural with the Form component.

.. _forms-embedding-single-object:

Embedding a Single Object
~~~~~~~~~~~~~~~~~~~~~~~~~

Expand Down Expand Up @@ -1263,6 +1265,7 @@ Next, add a new ``category`` property to the ``Task`` class::

/**
* @Assert\Type(type="Acme\TaskBundle\Entity\Category")
* @Assert\Valid()
*/
protected $category;

Expand All @@ -1279,6 +1282,12 @@ Next, add a new ``category`` property to the ``Task`` class::
}
}

.. tip::

The ``Valid`` Constraint has been added to the property ``category``. This
cascades the validation to the corresponding entity. If you omit this constraint
the child entity would not be validated.

Now that your application has been updated to reflect the new requirements,
create a form class so that a ``Category`` object can be modified by the user::

Expand Down Expand Up @@ -1326,16 +1335,7 @@ class:
}

The fields from ``CategoryType`` can now be rendered alongside those from
the ``TaskType`` class. To activate validation on CategoryType, add
the ``cascade_validation`` option to ``TaskType``::

public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'Acme\TaskBundle\Entity\Task',
'cascade_validation' => true,
));
}
the ``TaskType`` class.

Render the ``Category`` fields in the same way as the original ``Task`` fields:

Expand Down
9 changes: 7 additions & 2 deletions reference/forms/types/options/cascade_validation.rst.inc
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,13 @@ For example, if you have a ``ProductType`` with an embedded ``CategoryType``,
setting ``cascade_validation`` to ``true`` on ``ProductType`` will cause
the data from ``CategoryType`` to also be validated.

Instead of using this option, you can also use the ``Valid`` constraint in
your model to force validation on a child object stored on a property.
.. tip::

Instead of using this option, it is recommended that you use the ``Valid``
constraint in your model to force validation on a child object stored on
a property. This cascades only the validation but not the use of the
``validation_group`` option on child forms. You can read more about this
in the section about :ref:`Embedding a Single Object <forms-embedding-single-object>`.

.. include:: /reference/forms/types/options/_error_bubbling_hint.rst.inc