Support

Account

Home Forums ACF PRO Is it possible to apply validation to draft post?

Solved

Is it possible to apply validation to draft post?

  • Dear ACF PRO team

    I want to apply validation to draft post.

    (Of cause, validation to post in other statuses is working perfect.)

    Is it possible to do that?

  • Hi @hiroshi

    I believe you can do it by using the “acf/validate_value” hook. This page should give you more idea about it: http://www.advancedcustomfields.com/resources/acf-validate_value/.

    I hope this helps. Thanks!

  • Hi James!

    Thank you for your sonic reply!

    I’ve been using and loving ACF PRO, and has tried various kind of validations.
    But this time, I need a validation to a post saving newly as draft.

    All the validation works perfect when I save a post as publish,
    but none of them works when I do it as draft post.

    So I would appreciate any sorts of hint or tips to solve my problem above.

    Best regards,
    hiroshi

  • Hi @hiroshi

    Could you please share your validation code and the JSON or XML export of your field group so I can test it on my end?

    Thanks!

  • Hi @james

    I am very sorry for my long and impolite silence.

    Here’s my test validation code going with a ACF setting of 1 text field named as ‘test’.

    add_filter( 'acf/validate_value/name=test','my_acf_validatrion', 10, 4);
    function my_acf_validatrion( $valid, $value, $field, $input ){
    	if( !$valid || $value == ''  ) {
    		return true;
    	}
    	if ( $value != 'a' ) {
    		return 'bad input';
    	}
    	return $valid;
    }

    validation works
    – publish > publish
    – draft > publish

    validation doesn’t work
    – publish > draft
    – draft > draft
    – new > draft (newly posted one)

    I wanna know the behavior above is by design or not…

  • HI @hiroshi,

    Thanks for the follow up.

    This is the default working of the plugin. The plugin’s default “Required” check does not also return true for empty fields when saving drafts.

    I would recommend you have a look at this add on for ACF that makes use of server side validation and can also validate draft posts: https://wordpress.org/plugins/validated-field-for-acf/

    I hope this info helps.

  • Hi @james

    Thanks for your information again!

    I tried the plugin right away, but did not work…
    So I just wrote a report to the plugin’s author.

    I wish the function will be a part of ACF’s coming feature!

  • Hi @hiroshi,

    Thanks for the feedback.

    I would recommend you create a new feature request and send this to our support desk at [email protected] so that you this can get the attention it requires. This is because the plugin author rarely monitors the forum.

  • Hi @james

    As a temporaly solution,
    I modified 2 files of ACF core like below.

    Do you think this modification will cause system malfunction?

    /advanced-custom-fields-pro/assets/js/acf-input.min.js

    - 'click #save-post':'click_ignore',
    + 'click #save-post':'click_publish',
    

    /advanced-custom-fields-pro/forms/post.php

    - if( get_post_status($post_id) == 'publish' ) {
    + if( in_array(get_post_status($post_id), array('publish', 'future', 'draft', 'pending', 'private')) ) {
    

    PS
    Validation for posts beside publish is indispensable for 70% of my cliant cases.
    I wish coming ACF PRO will be able to select statuses to be validated.

  • Hi @hiroshi,

    I don’t think this would destabilize the plugin, the only disadvantage is that you will have to overwrite the function everytime ACF releases a new plugin update.

  • HI @james

    I’m gonna send this to your support desk later!

    Best regards

  • Hey,

    I know, this is from 2016.
    But today in 2022 I have the same problem.
    I have the newest ACF PRO Version.
    I have a custom post type for a user role.
    The user role can only save the CPT post as “pending”.
    The admin has to publish the CPT post.

    My problem is similar. The user with the specific user role can skip the required fields, because they are not validated on saving as “pending”. So the admin needs to contact the user to fill the required fields.

    Is there today a current solution?

    Best regards,
    Alex

  • Hi @razz
    Not sure, if this is still a concern, but here is what worked for me.
    Just removing event listener that caused ignoring of validation.
    Put this in your JS

    
    acf.addAction('prepare', function(){
        acf.validation.removeEvents({
            'click #save-post': 'onClickSave',
        });
    });
    

    or if you don’t know how or where, add this to your functions.php

    
    add_action('acf/input/admin_footer', function(){
        ?>
        <script>
            acf.addAction('prepare', function(){
                acf.validation.removeEvents({
                    'click #save-post': 'onClickSave',
                });
            });
        </script>
        <?php
    }, 10);
    
  • Hi @janousek-mi,

    thank you very much. I couldn’t get this to work!
    Is your code for frontend form?

    In backend it doesnt work for me. I put it into my js for the backend.

    best regards

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

The topic ‘Is it possible to apply validation to draft post?’ is closed to new replies.