From 47c755a202b1ea0b42e17675dfb743139cce7f13 Mon Sep 17 00:00:00 2001 From: Giovanni Gioffreda Date: Wed, 22 Nov 2023 14:17:34 +1300 Subject: [PATCH] Allow using the IsTrue validator against a simple form field By defaulting to the field value, we allow IsTrue validation against any form field, without hardcoded field name "g-recaptcha-response" in the request. This is useful for example: - when the "g-recaptcha-response" field cannot be sent with our request (neither through query string, or post, etc); - when a custom widget is rendering the field, the name of such field cannot be (or should not be) controlled; - when using third party UIs that support Symfony Forms and Validation but don't allow seting an out of scope variable to be sent to the server. Example: ```php $builder->add('_captcha', CaptchaType::class, [ 'required' => true, 'constraints' => [ new NotNull(), new IsTrue(), // verification token will be sent as "_captcha" ]); ``` --- src/Validator/Constraints/IsTrueValidator.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Validator/Constraints/IsTrueValidator.php b/src/Validator/Constraints/IsTrueValidator.php index 963329a..318e82a 100755 --- a/src/Validator/Constraints/IsTrueValidator.php +++ b/src/Validator/Constraints/IsTrueValidator.php @@ -102,7 +102,7 @@ public function validate($value, Constraint $constraint): void $remoteip = $request->getClientIp(); // define variable for recaptcha check answer - $answer = $request->get('g-recaptcha-response'); + $answer = $request->get('g-recaptcha-response', $value); // Verify user response with Google $response = $this->recaptcha->verify($answer, $remoteip);