|
| 1 | +PasswordStrength |
| 2 | +================ |
| 3 | + |
| 4 | +Validates that the given password has reached the minimum strength required by |
| 5 | +the constraint. The strength is measured using the `zxcvbn-php`_. library. |
| 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 | +.. include:: /reference/constraints/_groups-option.rst.inc |
| 78 | + |
| 79 | +``lowStrengthMessage`` |
| 80 | +~~~~~~~~~~~ |
| 81 | + |
| 82 | +**type**: ``string`` **default**: ``The password strength is too low. Please use a stronger password.`` |
| 83 | + |
| 84 | +The default message supplied when the password does not reach the minimum required score. |
| 85 | + |
| 86 | +.. include:: /reference/constraints/_payload-option.rst.inc |
| 87 | + |
| 88 | +``restrictedData`` |
| 89 | +~~~~~~~~~~~~~~~ |
| 90 | + |
| 91 | +**type**: ``string[]`` **default**: ``[]`` |
| 92 | + |
| 93 | +It is possible to determine if the submitted password contains restricted data |
| 94 | +such as the user given/family name, |
| 95 | + |
| 96 | +``restrictedDataMessage`` |
| 97 | +~~~~~~~~~~~~~ |
| 98 | + |
| 99 | +**type**: ``string`` **default**: ``The password contains the following restricted data: {{ wordList }}.`` |
| 100 | + |
| 101 | +The default message supplied when the password contains at least one restricted data. |
| 102 | + |
| 103 | +.. _`zxcvbn-php`: https://github.com/bjeavons/zxcvbn-php |
0 commit comments