@@ -129,11 +129,86 @@ The default message supplied when the password does not reach the minimum requir
129129 protected $rawPassword;
130130 }
131131
132- Learn more
133- ----------
132+ Customizing the Password Strength Estimation
133+ --------------------------------------------
134134
135- .. toctree ::
136- :maxdepth: 1
137- :glob:
135+ .. versionadded :: 7.2
138136
139- /validation/passwordStrength/*
137+ The feature to customize the password strength estimation was introduced in Symfony 7.2.
138+
139+ By default, this constraint calculates the strength of a password based on its
140+ length and the number of unique characters used. You can get the calculated
141+ password strength (e.g. to display it in the user interface) using the following
142+ static function::
143+
144+ use Symfony\Component\Validator\Constraints\PasswordStrengthValidator;
145+
146+ $passwordEstimatedStrength = PasswordStrengthValidator::estimateStrength($password);
147+
148+ If you need to override the default password strength estimation algorithm, you
149+ can pass a ``Closure `` to the :class: `Symfony\\ Component\\ Validator\\ Constraints\\ PasswordStrengthValidator `
150+ constructor (e.g. using the :doc: `service closures </service_container/service_closures >`).
151+
152+ First, create a custom password strength estimation algorithm within a dedicated
153+ callable class::
154+
155+ namespace App\Validator;
156+
157+ class CustomPasswordStrengthEstimator
158+ {
159+ /**
160+ * @return PasswordStrength::STRENGTH_*
161+ */
162+ public function __invoke(string $password): int
163+ {
164+ // Your custom password strength estimation algorithm
165+ }
166+ }
167+
168+ Then, configure the :class: `Symfony\\ Component\\ Validator\\ Constraints\\ PasswordStrengthValidator `
169+ service to use your own estimator:
170+
171+ .. configuration-block ::
172+
173+ .. code-block :: yaml
174+
175+ # config/services.yaml
176+ services :
177+ custom_password_strength_estimator :
178+ class : App\Validator\CustomPasswordStrengthEstimator
179+
180+ Symfony\Component\Validator\Constraints\PasswordStrengthValidator :
181+ arguments : [!service_closure '@custom_password_strength_estimator']
182+
183+ .. code-block :: xml
184+
185+ <!-- config/services.xml -->
186+ <?xml version =" 1.0" encoding =" UTF-8" ?>
187+ <container xmlns =" http://symfony.com/schema/dic/services"
188+ xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
189+ xsi : schemaLocation =" http://symfony.com/schema/dic/services https://symfony.com/schema/dic/services/services-1.0.xsd" >
190+
191+ <services >
192+ <service id =" custom_password_strength_estimator" class =" App\Validator\CustomPasswordStrengthEstimator" />
193+
194+ <service id =" Symfony\Component\Validator\Constraints\PasswordStrengthValidator" >
195+ <argument type =" service_closure" id =" custom_password_strength_estimator" />
196+ </service >
197+ </services >
198+ </container >
199+
200+ .. code-block :: php
201+
202+ // config/services.php
203+ namespace Symfony\Component\DependencyInjection\Loader\Configurator;
204+
205+ use Symfony\Component\Validator\Constraints\PasswordStrengthValidator;
206+
207+ return function (ContainerConfigurator $container): void {
208+ $services = $container->services();
209+
210+ $services->set('custom_password_strength_estimator', CustomPasswordStrengthEstimator::class);
211+
212+ $services->set(PasswordStrengthValidator::class)
213+ ->args([service_closure('custom_password_strength_estimator')]);
214+ };
0 commit comments