Support

Account

Home Forums General Issues validate_value filter doesn't work

Solving

validate_value filter doesn't work

  • Hi,
    I’m trying to validate fields using this docs:
    https://www.advancedcustomfields.com/resources/acf-validate_value/

    But no matter what I’ve tried, it doesn’t work.
    If I got this correctly, if I’m using this code, every time I save post with acf in it, I need to see “is that working?”:

    add_filter('acf/validate_value', 'my_acf_validate_value', 10, 4);
    
    function my_acf_validate_value( $valid, $value, $field, $input ){
    
    	return "is that working"?
    }

    To code above is just for testing, I’ve tried to do excaly as in the docs but nothing happen.
    *I registered the field through php, is that related?

    Thanks for the help!

  • This should be working. There are a couple of things, the first is that the actual code because there’s an error in it, it should be

    
    add_filter('acf/validate_value', 'my_acf_validate_value', 10, 4);
    
    function my_acf_validate_value( $valid, $value, $field, $input ){
    
    	return "is that working?";
    }
    

    and that error will cause a PHP error that should cause it to fail.

    The other thing that may have something to do with it is, where are you adding the above code? Is it in functions.php?

  • I didn’t understand the first one.
    What’s the different between the code you wrote and mine?

    And is, it in in function.php

    The PHP error should cause it to fail but in my case it doesn’t fail, it saves the post successfully even thought I want to return some message.


  • // yours
    return “is that working”?
    // mine
    return “is that working?”;
    `
    yours has a syntax error. That syntax will cause php to exit and the AJAX request to fail, when that happens the post is submitted. Because the validation is done through AJAX, php failing does not mean that the post won’t be submitted.

  • Hi John thank you for your help,

    The semicolon was missing only here in my question, I didn’t miss it in the actual code.
    And it is still not working.

    I just want to know that I understand that correctly – If I put the above code in function.php, everytime that I try to save post with acf fields in it I should get error message saying “is that working?” and the post shouldn’t be saved, right?

    Right now it’s not working, I put the following code in functions.php, and the posts saves successfully.

    add_filter('acf/validate_value', 'my_acf_validate_value', 10, 4);
    
    function my_acf_validate_value( $valid, $value, $field, $input ){
    
    	return "is that working?";
    }

    EDIT:

    Just try to run this in order to check if my site is even response to the code

    add_action('acf/validate_save_post', 'my_acf_validate_value', 10, 0);
    
    function my_acf_validate_value(  ){
    
    	die();
    }

    And the posts still saves as usual..
    I don’t know if that’s related but I registered the fields manually via php..

  • As far as your first question, yes, if that code is in functions.php no posts should be saved and all fields should show the error, if not then for some reason the filter is not being called, I don’t know of any reason why that would be the case with ACF activated and that code in your functions file.

    In your second piece of code, there isn’t a hook acf/validate_save_post, there is a acf/save_post hook.

  • I’m having a bit different issue with this filter. My validation is working, but I’m using this with a front end acf_form and when the validation error triggers it’s displaying the message on the full page and not as a error on the field. Is that expected? I am using this on a WYSIWYG field if that makes any difference.

  • @phylaxis when this happens it is usually because there is a PHP error generated during the AJAX request for the validation. Turn on debugging and error logging and then check in your error log to see what is causing the request to fail. https://codex.wordpress.org/WP_DEBUG

  • I actually have WP_DEBUG on and I’m not getting any error in the browser or in the PHP error logs. As I mentioned I’m using acf_form so this is a front-end form. Does that make any difference? What is being returned is a fully formed HTML response:

    <html xmlns="http://www.w3.org/1999/xhtml" lang="en-US"><head>
    	<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    	<meta name="viewport" content="width=device-width">
    	<meta name="robots" content="noindex,follow">
    	<title>Validation failed</title>
    	<style type="text/css">
    		html {
    			background: #f1f1f1;
    		}
    		body {
    			background: #fff;
    			color: #444;
    			font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen-Sans, Ubuntu, Cantarell, "Helvetica Neue", sans-serif;
    			margin: 2em auto;
    			padding: 1em 2em;
    			max-width: 700px;
    			-webkit-box-shadow: 0 1px 3px rgba(0,0,0,0.13);
    			box-shadow: 0 1px 3px rgba(0,0,0,0.13);
    		}
    		h1 {
    			border-bottom: 1px solid #dadada;
    			clear: both;
    			color: #666;
    			font-size: 24px;
    			margin: 30px 0 0 0;
    			padding: 0;
    			padding-bottom: 7px;
    		}
    		#error-page {
    			margin-top: 50px;
    		}
    		#error-page p {
    			font-size: 14px;
    			line-height: 1.5;
    			margin: 25px 0 20px;
    		}
    		#error-page code {
    			font-family: Consolas, Monaco, monospace;
    		}
    		ul li {
    			margin-bottom: 10px;
    			font-size: 14px ;
    		}
    		a {
    			color: #0073aa;
    		}
    		a:hover,
    		a:active {
    			color: #00a0d2;
    		}
    		a:focus {
    			color: #124964;
    		    -webkit-box-shadow:
    		    	0 0 0 1px #5b9dd9,
    				0 0 2px 1px rgba(30, 140, 190, .8);
    		    box-shadow:
    		    	0 0 0 1px #5b9dd9,
    				0 0 2px 1px rgba(30, 140, 190, .8);
    			outline: none;
    		}
    		.button {
    			background: #f7f7f7;
    			border: 1px solid #ccc;
    			color: #555;
    			display: inline-block;
    			text-decoration: none;
    			font-size: 13px;
    			line-height: 26px;
    			height: 28px;
    			margin: 0;
    			padding: 0 10px 1px;
    			cursor: pointer;
    			-webkit-border-radius: 3px;
    			-webkit-appearance: none;
    			border-radius: 3px;
    			white-space: nowrap;
    			-webkit-box-sizing: border-box;
    			-moz-box-sizing:    border-box;
    			box-sizing:         border-box;
    
    			-webkit-box-shadow: 0 1px 0 #ccc;
    			box-shadow: 0 1px 0 #ccc;
    		 	vertical-align: top;
    		}
    
    		.button.button-large {
    			height: 30px;
    			line-height: 28px;
    			padding: 0 12px 2px;
    		}
    
    		.button:hover,
    		.button:focus {
    			background: #fafafa;
    			border-color: #999;
    			color: #23282d;
    		}
    
    		.button:focus  {
    			border-color: #5b9dd9;
    			-webkit-box-shadow: 0 0 3px rgba( 0, 115, 170, .8 );
    			box-shadow: 0 0 3px rgba( 0, 115, 170, .8 );
    			outline: none;
    		}
    
    		.button:active {
    			background: #eee;
    			border-color: #999;
    		 	-webkit-box-shadow: inset 0 2px 5px -3px rgba( 0, 0, 0, 0.5 );
    		 	box-shadow: inset 0 2px 5px -3px rgba( 0, 0, 0, 0.5 );
    		 	-webkit-transform: translateY(1px);
    		 	-ms-transform: translateY(1px);
    		 	transform: translateY(1px);
    		}
    
    			</style>
    </head>
    <body id="error-page">
    	<p></p><h2>Validation failed</h2><ul><li><div style="text-align: center"><h3>Unable to update your profile</h3>
            <p>We were unable to update your profile because your "About Me" was longer than 515 characters.</p>
            <p><a href="javascript:void(0);" onclick="window.location.reload(true);" class="button">Back to profile</a></p></div></li></ul><p></p>
    
    </body></html>
  • Like I said, the only reason this should happen is if the ajax request fails, or never happens. Is the JavaScript running?

    1) Is acf_form_head() before get_header() and run before any html is output?
    2) Does your theme contain call to wp_head()
    3) Does your theme contain call to wp_foot()
    4) Are your deferring the loading of JavaScript or otherwise altering the way that JavaScript is loaded on the page?

  • Let me try to answer these:

    1) Yes.

    2) Yes

    3) Yes

    4) No.

  • No, there shouldn’t be any difference between front end and back. If I create a text field that is require field and I edit the post/page that the field is shown on front end or admin, I get the same result, a validation error without the page loading or showing the error page that you report.

    I did find one other thing that could be causing this, that is any output created by anything other than the proper JSON response to the AJAX query. In other words, something else somewhere is generating output. This problem can be almost impossible to track down. The best way to start is to deactivate all plugins except ACF and change to and unmodified version of one of the 20XX themes and then start adding things until you get the same issue and go from there.

  • Thanks I really appreciate the responses and help. I will start digging and if I discover anything report back here.

  • Keep in mind, when you find where to start looking, that this can be caused by something as simple as white-space. For example a space or return before the opening <?php tag in a file or after the ‘ ?>` tag at the end of a file or even the white-space generated by something like

    
    <?php // a line of code ?>
    <?php // another line of code ?>
    

    This is why I say you need to narrow this down to what plugin or maybe the theme.

  • Ok, sorry this project went dead for a while, but I’m back at it and still having the same issue. I tried disabling all plug-ins except ACF and changing the template, but I’m still having the same problem. I don’t know if it will help, but this is my add_filter code:

    function fe_profile_validate_about_me( $valid, $value, $field, $input ) {
        
        //only run in on profiles
        if( is_admin()  || ! get_post_type() == 'profiles' )
        {
            return $valid;
        }
    
        $cleaned_value = wp_strip_all_tags( $value );
    
        if( strlen( $cleaned_value ) > 515 ) {
            $valid = '<div style="text-align: center"><h3>Unable to update your profile</h3>
            <p>We were unable to update your profile because your "About Me" was longer than 515 characters.</p>
            <p><a href="javascript:void(0);" onclick="window.location.reload(true);" class="button">Back to profile</a></p></div>';
        }
    
        return $valid;
    }
    add_filter('acf/validate_value/name=fe-profile-short-description', 'fe_profile_validate_about_me', 10, 4);
  • I am experiencing exactly the same problem reported here. My validate value filters never get called. They are in my theme functions.php file and I have used the code provided above.

    I am seeing acf/save_post work correctly btw. But acf/validate_value never does anything. Any help please? How do I debug this.

    I am running a brand new WP site (as of a week ago) on PHP 5.4

    To isolate this issue I dropped my child theme and I am using plain twentyseventeen. The only change is the top of functions.php as shown here

    <?php
    
    add_filter('acf/validate_value', 'my_acf_validate_value', 10, 4);
    
    function my_acf_validate_value( $valid, $value, $field, $input ){
    
    	return "is that working?";
    }
    
    /**
     * Twenty Seventeen functions and definitions
     *
     * @link https://developer.wordpress.org/themes/basics/theme-functions/
     *
     * @package WordPress
     * @subpackage Twenty_Seventeen
     * @since 1.0
     */
  • I have duplicated this problem on a new & clean WP install. Here are the steps:
    1. Download WP from WP.org
    2. Install
    3. Add ACF plugin
    4. Add custom field group ‘foo’ with one text field ‘foo’
    5. edit functions.php for active theme and add the code from my previous post
    6. Add new post. See ‘foo’ field. Save new post. Filter does not run.
    7. change functions.php top to use acf/validate_save_post

    add_filter('acf/validate_save_post', 'my_acf_validate_value', 10, 0 );
    
    function my_acf_validate_value(  ){
    
    	return "is that working?";
    }

    8. Add new post and filter does not run
    9. Change functions.php top to use acf/save_post

    add_action('acf/save_post', 'my_acf_validate_value', 10, 0);
    
    function my_acf_validate_value(  ){
    
    	exit( 'save post' );
    }

    10. Add new post and action DOES run

    I have observed this with WP 4.9.4 and 4.8.5

    I have verified with this technique that the function is hooked — but it is not running. I have tried changing the priority up and down and that does not help.

    Please help. How do I get a the acf/validate_ filters to go? thank you!

  • Just an update for @beh333. I actually never resolved this issue with the filter. I needed up simply using a server side jQuery limiter and validation to work around the problem.

  • @beh333 Me too never resolved this issue, I just did custom jQuery form validation on $("#post").submit();

  • sorry for the lack of response here, but I still cannot duplicate this issue.

    one thing that I noticed in @beh333 comment is no reference to adding acf_form_head() https://www.advancedcustomfields.com/resources/acf_form_head/

  • I’m having the same issue as @phylaxis where the validation works but sends me to a separate page instead of inline AJAX. Console indicates 500 internal server error even for the most basic validation.

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

The topic ‘validate_value filter doesn't work’ is closed to new replies.