Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Doesn't work with sub-forms #7

Open
maartentibau opened this issue Dec 10, 2015 · 2 comments
Open

Doesn't work with sub-forms #7

maartentibau opened this issue Dec 10, 2015 · 2 comments

Comments

@maartentibau
Copy link

Hi,

Really nice that you made this! But when you work with sub-forms in ZF, the script doesn't work anymore. That's because Google generates it's own code for the input field (textarea) and that's always the POST param "g-recaptcha-response".

So when working with sub-forms you never retrieve the value of that field. I made a work-around by retrieving the raw request params and than perform a verify with the API (the same code that you use in the _verify method).

But just wanted to let you know this.. if you would find a more suitable solution that would be great of course.. I'll try myself, if I find something, I'll let you know!

@cgsmith
Copy link
Owner

cgsmith commented Dec 11, 2015

Thanks for reporting this! Do you have a simple sub-form gist you can post so I can test it out? Maybe myself or @elightbo can help out on this.

@maartentibau
Copy link
Author

It's pretty simple. When you create a sub-form with the name "example" .. the name of that input field becomes this:

<input id="example-g-recaptcha-response" name="example[g-recaptcha-response]" />

Now the javascript code of Goolge re-renders the input field to this

<textarea class="g-recaptcha-response" name="g-recaptcha-response" id="g-recaptcha-response"></textarea>

So.. as you can see, when you submit the form, the validation will always fail. I searched if it was possible to alter the name that ReCaptcha generates, but that isn't possible as far as I know.

Anyhow.. to make it all work I do this

... generate the form ...

//get the sub-form element
$elReCaptcha = $form->getSubForm('example')->getElement('g-recaptcha-response');

//when the a post request is being submit before we first retrieve the recaptcha param
if ($this->_request->isPost()) {
    $reCaptcha = (string) $this->_request->getParam('g-recaptcha-response');

    //now we verify the recaptcha (same code used as in the _verify in the recaptcha validator) if the
    //validation is successful we remove the validator from the element so it won't block the overall
    //validation of the form
    if (!empty($reCaptcha) && ReCaptcha::verify($reCaptcha)) {
        $elReCaptcha->removeValidator('ReCaptcha');
        $elReCaptcha->setAllowEmpty(true);
    }

    if ($form->isValid($this->_request->getParams())) {
        .....

        $form->reset();

        //at the end we need to add the recaptcha validator
        $elReCaptcha->addValidator('ReCaptcha');
        $elReCaptcha->setAllowEmpty(false);
    }
}

Anyhow.. I don't know if it's possible.. but of course it would be cool if there would be a solid solution for this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants