|
| 1 | +PasswordStrength |
| 2 | +================ |
| 3 | + |
| 4 | +Validates that the given password has reached the minimum strength required by |
| 5 | +the constraint. |
| 6 | + |
| 7 | +========== =================================================================== |
| 8 | +Applies to :ref:`property or method <validation-property-target>` |
| 9 | +Class :class:`Symfony\\Component\\Validator\\Constraints\\PasswordStrength` |
| 10 | +Validator :class:`Symfony\\Component\\Validator\\Constraints\\PasswordStrengthValidator` |
| 11 | +========== =================================================================== |
| 12 | + |
| 13 | +Basic Usage |
| 14 | +----------- |
| 15 | + |
| 16 | +The following constraint ensures that the ``rawPassword`` property of the |
| 17 | +``User`` class reaches the minimum strength required by the constraint. |
| 18 | +By default, the minimum required score is 2. |
| 19 | + |
| 20 | +.. configuration-block:: |
| 21 | + |
| 22 | + .. code-block:: php-attributes |
| 23 | +
|
| 24 | + // src/Entity/User.php |
| 25 | + namespace App\Entity; |
| 26 | +
|
| 27 | + use Symfony\Component\Validator\Constraints as Assert; |
| 28 | +
|
| 29 | + class User |
| 30 | + { |
| 31 | + #[Assert\PasswordStrength] |
| 32 | + protected $rawPassword; |
| 33 | + } |
| 34 | +
|
| 35 | + .. code-block:: yaml |
| 36 | +
|
| 37 | + # config/validator/validation.yaml |
| 38 | + App\Entity\User: |
| 39 | + properties: |
| 40 | + rawPassword: |
| 41 | + - PasswordStrength |
| 42 | +
|
| 43 | + .. code-block:: xml |
| 44 | +
|
| 45 | + <!-- config/validator/validation.xml --> |
| 46 | + <?xml version="1.0" encoding="UTF-8" ?> |
| 47 | + <constraint-mapping xmlns="http://symfony.com/schema/dic/constraint-mapping" |
| 48 | + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
| 49 | + xsi:schemaLocation="http://symfony.com/schema/dic/constraint-mapping https://symfony.com/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd"> |
| 50 | +
|
| 51 | + <class name="App\Entity\User"> |
| 52 | + <property name="rawPassword"> |
| 53 | + <constraint name="PasswordStrength"></constraint> |
| 54 | + </property> |
| 55 | + </class> |
| 56 | + </constraint-mapping> |
| 57 | +
|
| 58 | + .. code-block:: php |
| 59 | +
|
| 60 | + // src/Entity/User.php |
| 61 | + namespace App\Entity; |
| 62 | +
|
| 63 | + use Symfony\Component\Validator\Constraints as Assert; |
| 64 | + use Symfony\Component\Validator\Mapping\ClassMetadata; |
| 65 | +
|
| 66 | + class User |
| 67 | + { |
| 68 | + public static function loadValidatorMetadata(ClassMetadata $metadata) |
| 69 | + { |
| 70 | + $metadata->addPropertyConstraint('rawPassword', new Assert\PasswordStrength()); |
| 71 | + } |
| 72 | + } |
| 73 | +
|
| 74 | +Available Options |
| 75 | +----------------- |
| 76 | + |
| 77 | +``minScore`` |
| 78 | +~~~~~~~~~~~~ |
| 79 | + |
| 80 | +**type**: ``integer`` **default**: ``PasswordStrength::STRENGTH_REASONABLE`` (``2``) |
| 81 | + |
| 82 | +The minimum required strength of the password. Available constants are: |
| 83 | +* ``PasswordStrength::STRENGTH_WEAK`` = ``1`` |
| 84 | +* ``PasswordStrength::STRENGTH_REASONABLE`` = ``2`` |
| 85 | +* ``PasswordStrength::STRENGTH_STRONG`` = ``3`` |
| 86 | +* ``PasswordStrength::STRENGTH_VERY_STRONG`` = ``4`` |
| 87 | + |
| 88 | +``PasswordStrength::STRENGTH_VERY_WEAK`` is available but only used internally |
| 89 | +or by a custom password strength estimator. |
| 90 | + |
| 91 | +.. code-block:: php-attributes |
| 92 | +
|
| 93 | + // src/Entity/User.php |
| 94 | + namespace App\Entity; |
| 95 | +
|
| 96 | + use Symfony\Component\Validator\Constraints as Assert; |
| 97 | +
|
| 98 | + class User |
| 99 | + { |
| 100 | + #[Assert\PasswordStrength([ |
| 101 | + 'minScore' => PasswordStrength::STRENGTH_VERY_STRONG, // Very strong password required |
| 102 | + ])] |
| 103 | + protected $rawPassword; |
| 104 | + } |
| 105 | +
|
| 106 | +``message`` |
| 107 | +~~~~~~~~~~~~~~~~~~~~~~ |
| 108 | + |
| 109 | +**type**: ``string`` **default**: ``The password strength is too low. Please use a stronger password.`` |
| 110 | + |
| 111 | +The default message supplied when the password does not reach the minimum required score. |
| 112 | + |
| 113 | +.. code-block:: php-attributes |
| 114 | +
|
| 115 | + // src/Entity/User.php |
| 116 | + namespace App\Entity; |
| 117 | +
|
| 118 | + use Symfony\Component\Validator\Constraints as Assert; |
| 119 | +
|
| 120 | + class User |
| 121 | + { |
| 122 | + #[Assert\PasswordStrength([ |
| 123 | + 'message' => 'Le mot de passe est trop faible. Veuillez utiliser un mot de passe plus fort.' |
| 124 | + ])] |
| 125 | + protected $rawPassword; |
| 126 | + } |
0 commit comments