Skip to content

Commit f703400

Browse files
committed
PasswordStrength Documentation pages
1 parent e556fb3 commit f703400

File tree

5 files changed

+137
-0
lines changed

5 files changed

+137
-0
lines changed

reference/configuration/framework.rst

+8
Original file line numberDiff line numberDiff line change
@@ -2587,6 +2587,14 @@ metadata of the class. You can define an array of strings with the names of
25872587
several methods. In that case, all of them will be called in that order to load
25882588
the metadata.
25892589

2590+
.. _reference-validation-password-strength:
2591+
2592+
password_strength
2593+
.................
2594+
2595+
The :doc:`PasswordStrength </reference/constraints/PasswordStrength>`
2596+
constraint verifies the submitted string entropy is matching the minimum entropy score.
2597+
25902598
.. _reference-validation-email_validation_mode:
25912599

25922600
email_validation_mode

reference/constraints.rst

+1
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ Validation Constraints Reference
7575
constraints/All
7676
constraints/UserPassword
7777
constraints/NotCompromisedPassword
78+
constraints/PasswordStrength
7879
constraints/Valid
7980
constraints/Traverse
8081
constraints/CssColor

reference/constraints/Compound.rst

+1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ you can create your own named set or requirements to be reused consistently ever
3737
new Assert\Type('string'),
3838
new Assert\Length(['min' => 12]),
3939
new Assert\NotCompromisedPassword(),
40+
new Assert\PasswordStrength(['minScore' => 4]),
4041
];
4142
}
4243
}
+126
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
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+
}

reference/constraints/map.rst.inc

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ String Constraints
2828
* :doc:`Ulid </reference/constraints/Ulid>`
2929
* :doc:`UserPassword </reference/constraints/UserPassword>`
3030
* :doc:`NotCompromisedPassword </reference/constraints/NotCompromisedPassword>`
31+
* :doc:`PasswordStrength </reference/constraints/PasswordStrength>`
3132
* :doc:`CssColor </reference/constraints/CssColor>`
3233
* :doc:`NoSuspiciousCharacters </reference/constraints/NoSuspiciousCharacters>`
3334

0 commit comments

Comments
 (0)