advancedforms / advanced-forms

WordPress plugin to create forms using Advanced Custom Fields
75 stars 14 forks source link

Add Hook last page validation before submit #85

Closed cedricDevWP closed 4 years ago

cedricDevWP commented 4 years ago

Hello,

For a form, I am trying to perform an action before submitting the form and after verifying the information entered.

I looked in the function "validatePage" in form.js but I did not find a hook like for the function "changePage" ("af / form / page_changed").

Is this feature present? If not, do you have a way to achieve what I want to do? Is this a feature that might emerge?

I would like to set up this feature so that I can make a payment after verifying the information entered.

fabianlindfors commented 4 years ago

Hi, Cedric!

There currently is no such action in Advanced Forms because the plugin leaves the form validation and submission entirely to ACF.

Because of this, there should be an action/filter in ACF you could use instead. I can't say for sure which one would work but this one looks pretty good: https://www.advancedcustomfields.com/resources/javascript-api/#filters-validation_complete.

Let me know if that works! :)

cedricDevWP commented 4 years ago

Hello,

Your tip allowed me to do what I wanted.

If other people are interested here is my code :

acf.add_filter("validation_complete", function( json, form ){
   // check no errors and last page
   // Can be used when you want to make a payment, ex: stripe, when all the form is correct

    if( json.valid && af.pages.isLastPage(Object.values(af.forms)[0]) ) {
        json.valid = 0;
        json.errors = [{
            input   : "acf[field_5f3a77fc5dd23]",
            message : "Custom error"
        }];
       // DO SOMETHING
    }

    return json;  
});