bolt / forms

MIT License
21 stars 27 forks source link

error submit form contact with a captcha field #97

Closed Tomr38 closed 2 years ago

Tomr38 commented 2 years ago

hi everyone, when I submit my contact form with a captcha type field (I have all tried it), I have the following error: "An exception has been thrown during the rendering of a template ("Warning: in_array() expects parameter 2 to be array, null given")."

I applied exactly the configuration that there is here https://github.com/bolt/forms/blob/8149c9082422d9208c5ba57045599d3788ab7f9a/docs/captcha.md

any idea please ? thanks

bobdenotter commented 2 years ago

Hi, what's the full error message you're getting? (screenshots help)

Tomr38 commented 2 years ago

Capture d’écran 2022-02-07 164210

UtechtDustin commented 2 years ago

I had the same issue and figured out what the issue is. Each constraint must unset the groups unset($this->groups) at least one (https://github.com/symfony/symfony/issues/44846#issuecomment-1002617430_). This issue should also exists on the hcaptcha constraint i guess.

After that the issue is fixed, but then we get (depending on the recaptcha_type) the following error message

An exception has been thrown during the rendering of a template ("Warning: Undefined property: stdClass::$score").

This excpetion will be thrown because the RecaptchaService tries to get the score from the response, but the score only exists on v3 recaptchas not on v2.

v2 response (https://developers.google.com/recaptcha/docs/verify#api-response):

{
  "success": true|false,
  "challenge_ts": timestamp,  // timestamp of the challenge load (ISO format yyyy-MM-dd'T'HH:mm:ssZZ)
  "hostname": string,         // the hostname of the site where the reCAPTCHA was solved
  "error-codes": [...]        // optional
}

v3 response (https://developers.google.com/recaptcha/docs/v3#site_verify_response):

{
  "success": true|false,      // whether this request was a valid reCAPTCHA token for your site
  "score": number             // the score for this request (0.0 - 1.0)
  "action": string            // the action name for this request (important to verify)
  "challenge_ts": timestamp,  // timestamp of the challenge load (ISO format yyyy-MM-dd'T'HH:mm:ssZZ)
  "hostname": string,         // the hostname of the site where the reCAPTCHA was solved
  "error-codes": [...]        // optional
}

So the solution would be to check if the captcha_type is v2 and then ignore the score part.