Skip to content

[Validator] Use more realistic example #15383

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 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
46 changes: 23 additions & 23 deletions reference/constraints/Compound.rst
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ you can create your own named set or requirements to be reused consistently ever
.. configuration-block::

.. code-block:: php-annotations

// src/Validator/Constraints/PasswordRequirements.php
namespace App\Validator\Constraints;

Expand Down Expand Up @@ -69,7 +69,7 @@ you can create your own named set or requirements to be reused consistently ever
new Assert\NotCompromisedPassword(),
];
}
}
}

Add ``@Annotation`` or ``#[\Attribute]`` to the constraint class if you want to
use it as an annotation/attribute in other classes. If the constraint has
Expand All @@ -87,38 +87,38 @@ You can now use it anywhere you need it:

.. code-block:: php-annotations

// src/User/RegisterUser.php
namespace App\User;
// src/Entity/User.php
namespace App\Entity\User;

use App\Validator\Constraints as AcmeAssert;
use App\Validator\Constraints as Assert;

class RegisterUser
class User
{
/**
* @AcmeAssert\PasswordRequirements()
* @Assert\PasswordRequirements()
*/
public $password;
public $plainPassword;
}

.. code-block:: php-attributes

// src/User/RegisterUser.php
namespace App\User;
// src/Entity/User.php
namespace App\Entity\User;

use App\Validator\Constraints as AcmeAssert;
use App\Validator\Constraints as Assert;

class RegisterUser
class User
{
#[AcmeAssert\PasswordRequirements]
public $password;
#[Assert\PasswordRequirements]
public $plainPassword;
}

.. code-block:: yaml

# config/validator/validation.yaml
App\User\RegisterUser:
App\Entity\User:
properties:
password:
plainPassword:
- App\Validator\Constraints\PasswordRequirements: ~

.. code-block:: xml
Expand All @@ -129,26 +129,26 @@ You can now use it anywhere you need it:
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/constraint-mapping https://symfony.com/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd">

<class name="App\User\RegisterUser">
<property name="password">
<class name="App\Entity\User">
<property name="plainPassword">
<constraint name="App\Validator\Constraints\PasswordRequirements"/>
</property>
</class>
</constraint-mapping>

.. code-block:: php

// src/User/RegisterUser.php
namespace App\User;
// src/Entity/User.php
namespace App\Entity\User;

use App\Validator\Constraints as AcmeAssert;
use App\Validator\Constraints as Assert;
use Symfony\Component\Validator\Mapping\ClassMetadata;

class RegisterUser
class User
{
public static function loadValidatorMetadata(ClassMetadata $metadata)
{
$metadata->addPropertyConstraint('password', new AcmeAssert\PasswordRequirements());
$metadata->addPropertyConstraint('plainPassword', new Assert\PasswordRequirements());
}
}

Expand Down