Support

Account

Home Forums General Issues Error page when form submitted ajax validation failed

Solving

Error page when form submitted ajax validation failed

  • Hi,

    I try to use front-end forms.

    I registered form and use it in template.

    
    add_action('init', 'init_forms');
    
    function init_forms()
            form_register();
    }
    
    function form_register()
    {
        if (is_admin() || is_admin_login_page()) {
            return;
        }
    
        $pageAccount = get_page_with_shortcode('ACCOUNT_SHORTCODE');
    
        $permalinkAccount = get_permalink($pageAccount->ID);
        if (empty($permalinkAccount)) {
            $permalinkAccount = '';
        }
    
        if (check_current_url_equals_shortcode('ACCOUNT_SHORTCODE_REGISTER')) {
            acf_register_form(array(
                'id' => 'new_member',
                'post_id' => 'new_post',
                'new_post' => array(
                    'post_type' => 'member',
                    'post_status' => 'publish',
                ),
                'honeypot' => true,
    //            'field_groups' => array('group_64ae62c8e220b'),
                'fields' => [
                    LASTNAME_FIELD,
                    FIRSTNAME_FIELD,
                    EMAIL_FIELD,
                    EMAIL_FIELD . 'repeat',
                    PHONE_FIELD,
                    PASSWORD_FIELD,
                    PASSWORD_FIELD . 'repeat',
                    GENDER_FIELD,
                ],
                'form' => true,
                'updated_message' => __('Informations saved !', 'onlyweb'),
                'submit_value' => __('Save profile', 'onlyweb'),
                'html_submit_button' => '<input type="submit" class="btn acf-button button button-primary button-large" value="%s" />',
                'return' => add_query_arg(array('updated' => 'true'), $permalinkAccount),
            ));
        }
    }
    

    I added validation for field like exmple below :

    
    
    add_filter('acf/validate_value/key=' . EMAIL_FIELD . 'repeat', 'my_acf_validate_check_login_are_same', 10, 4);
    function my_acf_validate_check_login_are_same($valid, $value, $field, $input)
    {
        // bail early if value is already invalid
        if (!$valid) {
            return $valid;
        }
    
        if (!is_admin()) {
    
            // Remove all errors if user is an administrator.
            if (current_user_can('manage_options')) {
                acf_reset_validation_errors();
            }
    
            $login = (!empty($_POST['acf'][EMAIL_FIELD])) ? $_POST['acf'][EMAIL_FIELD] : false;
            $loginRepeat = (!empty($_POST['acf'][EMAIL_FIELD . 'repeat'])) ? $_POST['acf'][EMAIL_FIELD . 'repeat'] : false;
    
            if ($login != $loginRepeat) {
                $valid = __('Email are not the same !', 'onlyweb');
                $valid = 'error 2';
    
    //            return $valid;
            }
        }
    
        return $valid;
    }
    

    The problem appears when I submit the form, error are shown on an other error page.

    Any solution ?

  • This is because you have errors in your php code

    For example

    
    $login = (!empty($_POST['acf'][EMAIL_FIELD])) 
    

    Where is EMAIL_FIELD defined? This is probably generating an undefined constant warning that is causing the AJAX request to fail, unless you have these defined in some other code.

    also, you should be specifying either the field group or the fields, not both.

    When specifying the fields this needs to be an array of field keys, where are these constants defined? Are they field key values?

    
    'fields' => [
                    LASTNAME_FIELD,
                    FIRSTNAME_FIELD,
                    EMAIL_FIELD,
                    EMAIL_FIELD . 'repeat',
                    PHONE_FIELD,
                    PASSWORD_FIELD,
                    PASSWORD_FIELD . 'repeat',
                    GENDER_FIELD,
                ],
    

    If I am wrong and these constants are defined somewhere then you need to turn on error logging and find what is causing the error that is breaking the AJAX request.

  • Hello @hube2

    I found the solution here :

    After many tries I found the solution based on this answer : https://wordpress.stackexchange.com/questions/307794/error-in-validate-field-with-acf-plugin-in-wordpress#comment473230_319945

    I called

    if (!is_admin()) {

    Problem comes because of ajax use and is not admin function.

    Hope this help someone else 🙂

Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.