Support

Account

Home Forums Backend Issues (wp-admin) Validate one field against another

Solved

Validate one field against another

  • I would like to validate one field against another. Both fields are ACF fields.

    How do I get the value of the other field inside the acf/validate_value.. filter, since I don’t have the post id in order to retrieve the field using get_field, and without the second parameter get_field doesn’t appear to work.

    Thanks for any help. I’m probably doing something really stupid

  • Not realy.. I need to compare value if it exists in othere posts and if does then fail the validation, but since we don’t have $post_id i can’t exclude current post to check how many posts there are with meta_value. And if i don’t exclude curent id then when i do validation on new post it doesn’t have a meta_value set and get_posts finds only 1 post and validation is ok, so after validate_value i have 2 pots with same meta_value

    So if someone knows how can we get post_id in validate_value please tell us!

  • I found workaround:

    add_action( 'admin_footer', function() {
    	?>
    	<script type="text/javascript">
    		var tmp_acf_serialize_form = acf.serialize_form;
    		acf.serialize_form = function( $el, prefix ) {
    			var data = tmp_acf_serialize_form( $el, prefix );
    			data['post_id'] = <?php echo get_the_id(); ?>;
    			return data;
    		}
    	</script>
    	<?php
    } );

    and now in add_filter( 'acf/validate_value/... you can use $_REQUEST['post_id'];

    There’s needs to be checked if acf exists in js…

  • I found a solution myself. Access $_POST[‘acf’][‘field_xxxxxxxx’]

    Tricky to find! Thanks for the other hints.

  • To be sure to have the good reference, it’s possible to get it with this :
    $acf_ref = get_post_meta( $post_id, '_my_field_name', true );

    And then use $_POST[‘acf’][$acf_ref] to have the posted value.

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

The topic ‘Validate one field against another’ is closed to new replies.