Skip to content

[Validator] feat : add password strength estimator related documentation #19910

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
9 changes: 9 additions & 0 deletions reference/constraints/PasswordStrength.rst
Original file line number Diff line number Diff line change
Expand Up @@ -128,3 +128,12 @@ The default message supplied when the password does not reach the minimum requir
])]
protected $rawPassword;
}

Learn more
----------

.. toctree::
:maxdepth: 1
:glob:

/validation/passwordStrength/*
48 changes: 48 additions & 0 deletions validation/passwordStrength/get_or_override_estimate_strength.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
How to Get or Override the Default Password Strength Estimation Algorithm
=========================================================================

Within the :class:`Symfony\\Component\\Validator\\Constraints\\PasswordStrengthValidator` a `dedicated function`_ is used to estimate the strength of the given password. This function can be retrieved directly from the :class:`Symfony\\Component\\Validator\\Constraints\\PasswordStrengthValidator` class and can also be overridden.

Get the default Password strength
---------------------------------

In case of need to retrieve the actual strength of a password (e.g. compute the score and display it when the user has defined a password), the ``estimateStrength`` `dedicated function`_ of the :class:`Symfony\\Component\\Validator\\Constraints\\PasswordStrengthValidator` is a public static function, therefore this function can be retrieved directly from the `PasswordStrengthValidator` class.::

use Symfony\Component\Validator\Constraints\PasswordStrengthValidator;

$passwordEstimatedStrength = PasswordStrengthValidator::estimateStrength($password);


Override the default Password strength estimation algorithm
-----------------------------------------------------------

If you need to override the default password strength estimation algorithm, you can pass a ``Closure`` to the :class:`Symfony\\Component\\Validator\\Constraints\\PasswordStrengthValidator` constructor. This can be done using the :doc:`/service_container/service_closures`.

First, create a custom password strength estimation algorithm within a dedicated callable class.::

namespace App\Validator;

class CustomPasswordStrengthEstimator
{
/**
* @param string $password
*
* @return PasswordStrength::STRENGTH_*
*/
public function __invoke(string $password): int
{
// Your custom password strength estimation algorithm
}
}

Then, configure the :class:`Symfony\\Component\\Validator\\Constraints\\PasswordStrengthValidator` service to use the custom password strength estimation algorithm.::

# config/services.yaml
services:
custom_password_strength_estimator:
class: App\Validator\CustomPasswordStrengthEstimator

Check failure on line 43 in validation/passwordStrength/get_or_override_estimate_strength.rst

View workflow job for this annotation

GitHub Actions / Code Blocks

[PHP syntax] Syntax error, unexpected ':', expecting T_STRING

Symfony\Component\Validator\Constraints\PasswordStrengthValidator:
arguments: [!service_closure '@custom_password_strength_estimator']

Check failure on line 46 in validation/passwordStrength/get_or_override_estimate_strength.rst

View workflow job for this annotation

GitHub Actions / Code Blocks

[PHP syntax] Syntax error, unexpected T_CONSTANT_ENCAPSED_STRING, expecting ',' or ']' or ')'

.. _`dedicated function`: https://github.com/symfony/symfony/blob/85db734e06e8cb32365810958326d48084bf48ba/src/Symfony/Component/Validator/Constraints/PasswordStrengthValidator.php#L53-L90
Loading