Support

Account

Home Forums ACF PRO Validate 2 fields

Solved

Validate 2 fields

  • Hi there,

    Basically, I would like to allow saving a post only if one of two specific fields is not null.

    I don’t think I can do that with ACF directly on the dashboard. I probably need to use filter.
    I found the acf-validate_value filter, but I can’t figure out how to check two fields in this function.

    Any idea on how I can perform this check?

    Many thanks!

  • All of the field data will be contained in $_POST['acf'] I think. I have not tested this, it is only based on my assumption.

    The indexes of this array are the field key values of the fields. It would depend on your fields. But you can get the value of both fields from the $_POST array and check to make sure that they are both not empty.

    
    add_filter('acf/validate_value/name=first_field_name', 'my_acf_validate_value', 10, 4);
    add_filter('acf/validate_value/name=second_field_name', 'my_acf_validate_value', 10, 4);
    
    function my_acf_validate_value( $valid, $value, $field, $input ){
    	
    	// bail early if value is already invalid
    	if( !$valid ) {
    		return $valid;
    	}
            // get two values
            // you need to change these based on your field keys
    	$value_1 = $_POST['acf']['field_123abc789']
            $value_2 = $_POST['acf']['field_987cba321']
    	if (empty($value_1) && empty($value_2))  {
                $valid = 'Please complete at least one of the fields';
            }
    	// return
    	return $valid;
    	
    	
    }
    
  • Perfect, thanks a lot !!

  • This Code Should be in function.php file?

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

The topic ‘Validate 2 fields’ is closed to new replies.