When a user types in an incorrect response into the Captcha challenge, with the 'preprocess' option, is it possible to pass the returned value (whether they previously answered incorrectly) to the 'generate' operation?

The reason I'm asking is so that reCAPTCHA could present the error in the challenge form itself instead of using form_set_error(). Like what you see when you enter an incorrect answer here.

Comments

soxofaan’s picture

Avoiding form_set_error() seems not a good idea, I think. If you don't use it during validation, Drupal won't know there is an error and will accept the submission.

robloach’s picture

Not instead of, but in addition to form_set_error. That's beside the point though.... Is there a way to return the result of preprocess to the generate operation of hook_captcha?

soxofaan’s picture

Is there a way to return the result of preprocess to the generate operation of hook_captcha?

Do you want the result of the preprocessing (which is the response to validate) or the result of the validation (which is 'success'/'failed')?
Do you want do it through the API? You could for example do some housekeeping of your own in $_SESSION.
Or do you want to put this in captcha.module independently from ReCAPTCHA?

It could make sense to provide the generate hook with information about the previous submission attempts (if any). The difficulty is that captchas are per form instance (identified by form_id and captcha_token). Captchas from subsequent form requests are unrelated and don't share information. Also the validation result (success/fail) is not saved in any way.

soxofaan’s picture

I tried some stuff and found out that with Drupal the form generation (and thus captcha generation) is done before validation of the (previously) submitted form. This is done because drupal needs the form structure before it can validate the submission. So during generation you can't now if validation of the previous post passed (unless you pull in the posted values and do some validation yourself)

I think it won't be easy (and at least very ugly) to do what you want to do.

robloach’s picture

Yeah, I tried some stuff out with $_SESSION and it wasn't doing it... What a pain...

One way of doing it would be using drupal_goto() along with $_SESSION, but that's horribly ugly. You have any ideas?

robloach’s picture

Do you want the result of the preprocessing (which is the response to validate) or the result of the validation (which is 'success'/'failed')?

I think passing what's returned from 'preprocess' to 'generate' would make the most sense. Then you could pass your own custom error flags and stuff.

Do you want do it through the API?

I attempted doing it without modifying the API (even that wasn't possible), but then I realized that other Captchas might also benefit from having the extra parameter passed to the 'generate' operation.

soxofaan’s picture

Making it possible through the API is a no go, I think. Like I previously explained, with Drupal there is no "clean" way possible for knowing during form generation if validation of the previous post was successful. I don't think it is a good idea to pollute the base captcha module with ugly/hackish code.

However, I had the following idea: with an additional pre_render procedure it should be possible to detect the error state (with form_get_errors()) and add some stuff to your form elements. The problem is that you have to set the #pre_render attribute at the toplevel of $form, so there is some API change needed.

robloach’s picture

Haha, this could even lead to a multi-step captcha system for Captcha Pack..... They'd have to answer one question, hit submit, and then answer the second captcha question, in order to fully submit the form.

soxofaan’s picture

They'd have to answer one question, hit submit, and then answer the second captcha question, in order to fully submit the form.

I wouldn't do that.
I guess most human users would find this annoying behavior. (I would).
CAPTCHAs are already experienced as an annoying hurdle, so I would try to keep the annoyance (for humans) to a minimum.

robloach’s picture

It was meant as a joke ;-) .

soxofaan’s picture

Status: Active » Closed (fixed)

closing old outdated issue