I've been experiencing a problem where the CAPTCHA on a form disappears after an ajax submission is triggered for the form by an addressfield widget. I select a new country, there's an ajax submission to update the addressfield fields for that country, and the CAPTCHA test disappears.

I've traced the cause of this to the fact that the captcha element's #process callback is getting called twice during the ajax submission (probably indicating that the form is getting rebuilt twice as well) and so what happens is this: The javascript settings added inside recaptcha_captca() get added twice, and they get merged by Drupal with array_merge_recursive() inside ajax_render(). That results in Drupal.settings.recaptcha containing this:

{
  theme: ["clean", "clean"],
  lang: ["en", "en"],
  public_key: ["....", "...."]
  container: ["recaptcha_ajax_api_container", "recaptcha_ajax_api_container"]
}

... instead of this:

{
  theme: "clean",
  lang: "en",
  public_key: "....",
  container: "recaptcha_ajax_api_container"
}

Obviously this breaks things. I have resolved this by using a static variable to ensure that the drupal_add_js() calls only happen once per captcha session per request. I'll provide a patch for this.

CommentFileSizeAuthor
#1 recaptcha-js-once-2231999-1.patch1.85 KBjamesharv
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

jamesharv’s picture

Assigned: jamesharv » Unassigned
Status: Needs work » Needs review
FileSize
1.85 KB

Patch attached

Liam Morland’s picture

Thanks for the patch. This seems to make sense, but I don't understand the source of the $session_id. The documentation for hook_captcha() doesn't state that a number like that is sent as the last argument. Is it really needed? What about just having a static variable that is set to true to first time through and, if it is true, don't put in the JS again?

This requires a re-roll due to #2232701: Use proper function signature for recaptcha_captcha().

Liam Morland’s picture

Status: Needs review » Needs work
myat’s picture

I am also facing same problem in reCapture 7.x-2.0.

The problem I have is after ajax form submission, capture is disappearing whether I put correct answer or wrong answer on captcha. Seems like reCaptcha didn't render again after AJAX form submit.

Liam Morland’s picture

Version: 7.x-1.x-dev » 7.x-2.x-dev

Let's get this solved in 2.x first, then we can see if the fix will backport.

hass’s picture

Version: 7.x-2.x-dev » 7.x-1.x-dev

There is already a case open, but it requires a totally different solution. Feel free to close if 1.x do not get maintained any longer.

artfulrobot’s picture

Is there any progress on this? I've got this problem with 7.x-2.1.

Liam Morland’s picture

This issue is about 7.x-1.x. As hass said in #6, there is another issue for 7.x-2.x.

hass’s picture

Status: Needs work » Closed (outdated)
madelyncruz’s picture

Component: reCAPTCHA Captcha » General

I'm also having this error with 7.x-2.x-dev

Liam Morland’s picture

See comment #6.