advancedforms / advanced-forms

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

fields missing on ACF 'validation_complete' filter after version 1.7.1 update #89

Closed DjoleBuk closed 3 years ago

DjoleBuk commented 3 years ago

Hello,

I was using the ACF 'validation_complete' filter for doing a logging and recap of all the previously enabled steps.

acf.add_filter('validation_complete', function( json, $form ){
    // check errors
    if( !json.errors ) {
        let formdata = $form.serializeArray();
        // do stuff with formdata
        // ...
    }
    return json;
});

This was working fine until version 1.7.1 and the changes made in the assets/js/form.js file. Now some fields are detached at the moment when the filter is executed, so we only get the fields present in the current step with $form.serializeArray().

This might be a desired feature but in my case it broke the current process. A simple setTimeout resolved the issue but I figured out, I could mention it here for future reference.

Code that resolved the issue (probably not optimal):

acf.add_filter('validation_complete', function( json, $form ){
    // check errors
    if( !json.errors ) {
        setTimeout(() => {
            let formdata = $form.serializeArray();
            // do stuff with formdata
            // ...
        }, 50);
    }
    return json;
});
fabianlindfors commented 3 years ago

Hi!

1.7.1 introduced some changes to make validation more reliable in multi-step forms. Sorry that this broke your use case! There is an official action for this: https://advancedforms.github.io/actions/js-af-form-page_changed/ but I think this might not actually work for your use case as the action is triggered after the fields have changed. I'll add it here though for future reference.

As this breakage wasn't part of the public API I'll close this issue for now. :)