advancedforms / advanced-forms

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

$fields is empty because of absent form field prefix 'acf' #12

Closed davidwebca closed 7 years ago

davidwebca commented 7 years ago

Hi!

I've detected a problem with ACF 5.3.10 and Advanced Forms 1.3.2

In core-forms.php, the $fields array is empty in the "pre_form" which makes the e-mail sent empty and no entry data is saved. I've narrowed it down to the function looking in a "acf" key for the form's data, but in the form I'm look at, there's no "acf" prefix to the input names. My guess is that it's a breaking change added by ACF by ACF and that "acf_render_field" function doesn't add that prefix anymore which you relied on, but now it causes the form submission to fail.

core-forms.php:93

Right now, the field names are plainly their field IDs, so I made a little function to fix this, but as I don't know all your code base and all the implications this could have, I didn't bother creating a pull request. Maybe there are other places in the code that are breaking because of this, so I'm simply using this issue here to let you know. I think it breaks the "required" field validation, but I'm not sure.

Here's how I personally fixed it temporarly by filtering the data in my functions.php file.

add_action('af/form/submission', function($form, $fields, $args){
    // Fix acf fields being empty
    $fields = array();
    foreach ($_POST as $key => $value) {
        if(strpos($key, 'field_') !== false){
            $field = acf_get_field( $key );
            $field['_input'] = $value;
            $field['value'] = acf_format_value( $value, 0, $field );
            $fields[] = $field;
        }
    }

    AF()->submission = array(
        'form' => $form,
        'args' => $args,
        'fields' => $fields,
    );
}, 1, 3);
davidwebca commented 7 years ago

Better fix : wrap the fields names / keys with "acf[]" before rendering. This fixes the ajax validation.

add_filter('acf/get_fields', function($fields, $parent){
    if(is_admin())
        return;

    foreach ($fields as $index => $field) {
                // Just a simple validation to make sure not to do
                // this to every fields as it potentially could break acf
        if(isset($field['parent']) && $field['parent'] == 'group_59776b3ecb8e8'){
            $fields[$index]['key'] = 'acf[' . $field['key'] . ']';
        }
    }

    return $fields;
}, 100, 2);
fabianlindfors commented 7 years ago

Hi, David! Thanks a ton for notifying me of this issue, much appreciated.

I'm currently running ACF 5.5.14 with AF 1.3.2 and not experiencing the problem. Is the problem limited to ACF 5.3? Could you try upgrading to 5.5 and see if the problem persists?

davidwebca commented 7 years ago

Hi @Fabianlindfors. In fact, upon receiving your reply, I saw that ACF was at 5.6.1. Updated to it, removed my filter and everything is fine now. Don't know what caused the problem or incompatibility between those two versions. You can close the issue and if anyone finds themselves with a pair of 5.3.10 and 1.3.2, they'll at least find it documented. Sorry for the trouble!

fabianlindfors commented 7 years ago

No worries! I'm glad you made me aware of the incompatibility. If I find more users running ACF 5.3 and lower I might patch it but I'll leave it for now.

Thanks!