Skip to content

[Validator] Deprecate annotations #18781

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
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
48 changes: 45 additions & 3 deletions components/validator/resources.rst
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,14 @@ configure the locations of these files::
The AnnotationLoader
--------------------

At last, the component provides an
.. deprecated:: 6.4

The :class:`Symfony\\Component\\Validator\\Mapping\\Loader\\AnnotationLoader`
is deprecated since Symfony 6.4, use the
:class:`Symfony\\Component\\Validator\\Mapping\\Loader\\AttributeLoader`
instead.

The component provides an
:class:`Symfony\\Component\\Validator\\Mapping\\Loader\\AnnotationLoader` to get
the metadata from the annotations of the class. Annotations are defined as ``@``
prefixed classes included in doc block comments (``/** ... */``). For example::
Expand Down Expand Up @@ -121,8 +128,44 @@ If you use annotations instead of attributes, it's also required to call
To disable the annotation loader after it was enabled, call
:method:`Symfony\\Component\\Validator\\ValidatorBuilder::disableAnnotationMapping`.

.. deprecated:: 6.4

The :method:`Symfony\\Component\\Validator\\ValidatorBuilder::enableAnnotationMapping`
and :method:`Symfony\\Component\\Validator\\ValidatorBuilder::disableAnnotationMapping`
methods are deprecated since Symfony 6.4, use the
:method:`Symfony\\Component\\Validator\\ValidatorBuilder::enableAttributeMapping`
and :method:`Symfony\\Component\\Validator\\ValidatorBuilder::disableAttributeMapping`
methods instead.

.. include:: /_includes/_annotation_loader_tip.rst.inc

The AttributeLoader
-------------------

.. versionadded:: 6.4

The :class:`Symfony\\Component\\Validator\\Mapping\\Loader\\AttributeLoader`
was introduced in Symfony 6.4.

The component provides an
:class:`Symfony\\Component\\Validator\\Mapping\\Loader\\AttributeLoader` to get
the metadata from the attributes of the class. For example::

use Symfony\Component\Validator\Constraints as Assert;
// ...

class User
{
#[Assert\NotBlank]
protected string $name;
}

To enable the attribute loader, call the
:method:`Symfony\\Component\\Validator\\ValidatorBuilder::enableAttributeMapping` method.

To disable the annotation loader after it was enabled, call
:method:`Symfony\\Component\\Validator\\ValidatorBuilder::disableAttributeMapping`.

Using Multiple Loaders
----------------------

Expand All @@ -136,8 +179,7 @@ multiple mappings::
use Symfony\Component\Validator\Validation;

$validator = Validation::createValidatorBuilder()
->enableAnnotationMapping(true)
->addDefaultDoctrineAnnotationReader()
->enableAttributeMapping()
->addMethodMapping('loadValidatorMetadata')
->addXmlMapping('validator/validation.xml')
->getValidator();
Expand Down
2 changes: 1 addition & 1 deletion form/unit_testing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ allows you to return a list of extensions to register::

// or if you also need to read constraints from annotations
$validator = Validation::createValidatorBuilder()
->enableAnnotationMapping(true)
->enableAttributeMapping()
->getValidator();

return [
Expand Down
24 changes: 24 additions & 0 deletions reference/configuration/framework.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2671,6 +2671,18 @@ enable_annotations

If this option is enabled, validation constraints can be defined using annotations or attributes.

.. deprecated:: 6.4

This option is deprecated since Symfony 6.4, use the ``enable_attributes``
option instead.

enable_attributes
.................

**type**: ``boolean`` **default**: ``true``

If this option is enabled, validation constraints can be defined using attributes.

translation_domain
..................

Expand Down Expand Up @@ -2862,6 +2874,18 @@ enable_annotations

If this option is enabled, serialization groups can be defined using annotations or attributes.

.. deprecated:: 6.4

This option is deprecated since Symfony 6.4, use the ``enable_attributes``
option instead.

enable_attributes
.................

**type**: ``boolean`` **default**: ``true``

If this option is enabled, serialization groups can be defined using attributes.

.. seealso::

For more information, see :ref:`serializer-using-serialization-groups-attributes`.
Expand Down