Support

Account

Home Forums Front-end Issues Submit frontend form even if validation fails

Solving

Submit frontend form even if validation fails

  • Hello.
    I searched and experimented a lot, but didn’t find a solution yet.
    I have a long acf-frontend-form with many required fields. When the users submit the form, it should be saved as a draft, even if validation fails. This for that they dont’s lose any data and are not obliged to fill the form at once.
    How can I submit a form (save it to th db as draft) and after still show validation messages?

    According to the acf-validate_save_post documentation it is possible to bypass validation with acf_reset_validation_errors();. Would it be possible to submit the form after again, for showing validation messages? How?

    In the following code I’m just tring to save the form with do_action('acf/save_post', $post_id); but this is not working.
    The code should save a post as a draft, if not validated and save it as publish if validated. I’m checking the validation status with the global variable $form_valid and the acf/validate_value-hook. This seems to work.

    Is this a way to go? Is the concept worng? Is there an other solution? What am I missing? I’d be glad for some hints. Thanks a lot.

    // Auch Entwürfe mit single.php anzeigen
    function ceo_single_page_published_and_draft_posts( $query ) {
        if( is_single() ) {
            $query->set('post_status', 'publish,draft');
        }
    }
    add_action('pre_get_posts', 'ceo_single_page_published_and_draft_posts');
    
    // ACF-Validation: Post als Draft speichern, falls nicht valid
    // globale Variable definieren
    $form_valid=true;
    $loop_breaker=false;
    // Check if all fields are valid (save it in global variable $form_valid)
    function my_acf_validate_value( $valid, $value, $field, $input ){
        global $form_valid;
        if( !$valid ) {
            $form_valid = false;
        }
        return $valid;
    }
    add_filter('acf/validate_value', 'my_acf_validate_value', 10, 4);
    
    function my_acf_validate_save_post() {
        global $form_valid;
        global $form_validieren;
        
        if( !is_admin() ):
    
            $post_id = get_the_ID();
            $the_post = get_post( $post_id );
    
            if( !$form_valid and !$loop_breaker):
                // Wenn Validation nicht erfolgreich, als draft speichern
                $the_post->post_status = 'draft';
                wp_update_post( $the_post );
    
                $loop_breaker=true;
                // Form erneut abschicken, für Validierung
                do_action('acf/save_post', $post_id);   // (not working)
            elseif( $form_valid and !$loop_breaker):
                // Andernfalls "veröffentlichen", falls nötig (draft)
                if ($the_post->post_status = 'draft'):
                    $the_post->post_status = 'publish';
                    wp_update_post( $the_post );
                endif;
            elseif ($loop_breaker):
                $loop_breaker=false;
                $form_valid=true;    // zurücksetzen
            endif;
        endif;
    }
    add_action('acf/validate_save_post', 'my_acf_validate_save_post', 10, 0);
  • Before I start, let me say that I don’t know anything about the functions that you mention and that this is not a solution but a direction for you to look in.

    I don’t think that you can stop ACF from validating fields and showing errors and make it allow the submission of the form when there are errors. The first step will be that you need to remove all of the settings for fields that require fields, you simply will not be able to use the validation.

    The next step will be for you to create your own ac/pre_save_post filter https://www.advancedcustomfields.com/resources/acf-pre_save_post/, or you can probably use the acf/save_post filter. This depends on what you want to do. If you want to save a post as a draft or published based on validation then I’d probably go with pre_save_post, but the other can be used just as easily as you and always update the post status based on validation as well.

    Anyway, if I understand what acf_add_validation_errro() does, in your filter you would do all the validation yourself and use this function set all errors for all fields yourself.

    Hope that helps you get to where you’re going.

  • Thanks for your post and considerations.
    To bypass the vaidation is easy, with acf_reset_validation_errors(); (see example in https://www.advancedcustomfields.com/resources/acf-validate_save_post/). The tricky thing for me now is to submit the form two times: one time with bypassing validation for saving the post-edit and the second time showing the validation. I try to figure this out with an automatic javascript submit after the reload of a first submit. But mabe it gets complicated, since I’m not too experienced. I’ll have a closer look at your idea too, creating my own validation with acf_add_validation_errro();. I’ll post what my solution will be, but need some time.
    An other idea is to install an automatic submit as draft say every minute with javascript setInterval. Then I’d need to find a way to submit without reloading the page.
    If anyone has more hints how to go, I’ll appreciate it. Greetings.

  • @bru500, Did you ever figure this out.

    I’m trying to do something similar, although acf_reset_validation_errors isn’t working for me. It posts but nothing saves and the front end gets hung up on required fields.

    Any ideas?

  • In the upcoming 5.7 you can turn off validation via:

    window.acf.validation.active = false;

    You also need to set the attribute formnovalidate to true on the submit button to avoid html5 validation.

  • Hi there,

    I’m also needing a front end acf_form() to NOT validate. I have a massive form which has a ‘save for later’ option. Obviously with required fields this cannot work.

    I have tried the

    window.acf.validation.active = false;

    and set the submit button to

    formnovalidate

    what happens now is I no longer get the HTML5 validation on the page, but it now moves to a new page with a window that displays the fields that are not validated.

    thus
    —————————-
    Validation failed

    Project Title value is required
    Sub-Specialty value is required
    Current CV value is required
    —————————-

    So the form is still not saved unless these fields are populated.

    It is at all possible to disable validation on a front end acf_form() ?

    Thanks in advance and apologies if I have missed this solution elsewhere….

    NK

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

You must be logged in to reply to this topic.