getgrav / grav-plugin-form

Grav Form Plugin
http://getgrav.org
MIT License
53 stars 79 forks source link

AJAX forms do not work anymore with captcha #512

Open kaphadley opened 3 years ago

kaphadley commented 3 years ago

Hi,

AJAX forms (as described in the documentation) do not work anymore when the form contains a type: captcha field.

Ajax and captcha work fine wight grav-plugin-form v3.0.8, but fails from at least v4.0.0.

marcocevoli commented 3 years ago

Same issue here.

rhukster commented 3 years ago

we'll take a look at this...

dledanseur commented 3 years ago

This is due to the captcha integration bypassing our own event handlers by submitting the form. As a workaround, you can override the form's submit function. For instance, adapting the documentation:

const form = document.querySelector('#ajax-test-form');
form.submit = function() {

    const result = document.querySelector('#form-result');
    const action = form.getAttribute('action');
    const method = form.getAttribute('method');

    fetch(action, {
        method: method,
        body: new FormData(form)
    })
    .then(function(response) {
        if (response.ok) {
            return response.text();
        } else {
            return response.json();
        }
    })
    .then(function(output) {
        if (result) {
            result.innerHTML = output;
        }
    })
    .catch(function(error) {
        if (result) {
            result.innerHTML = 'Error: ' + error;
        }

        throw new Error(error);
    });
}
kaphadley commented 3 years ago

@dledanseur your code is the same as the one from the documentation, except the missing event.preventDefault();. How is it supposed to solve the issue?

dledanseur commented 3 years ago

@dledanseur your code is the same as the one from the documentation, except the missing event.preventDefault();. How is it supposed to solve the issue?

Not exactly the same, the key point here is to override the form's submit function with form.submit =

marcocevoli commented 3 years ago

Any news about this. We're getting loads of spam because we needed to disable the captcha...

01Kuzma commented 2 years ago

More than a year has passed since the last answer... Spam is a real problem. Any news? Moreover it doesn't work with dropzone