Support

Account

Home Forums Feature Requests Validation of a field based on the value of another field

Solved

Validation of a field based on the value of another field

  • It would be really useful to when using the acf/validate_value/ hook to be able to validate the field’s value based upon the new value of another field.

    For example:

    • A post exists which has both a start date field and an end date field
    • The post is edited and both of these dates are changed
    • Use the hook acf/validate_value/ on the end date field and check that the newly entered end date is after the newly entered start date
  • Actually, this can be done. I’m in the middle of a project where I needed to validate multiple fields to make sure that a duplicate post is not created.

    ACF passes the value of the field in question but $_POST[‘acf’] contains all of the fields that are submitted. Each index of this array is the field key.

    Using the field key you can get the value submitted in the other field and use it to validate the current field.

    Hope this helps point you in the right direction.

    I’d post my functions but they’d make very little sense in this case because I’m validated 5 fields and I’m validating them against existing posts rather then the value of just one other field.

  • For others looking for this, here is some example code

    
    add_filter('acf/validate_value/key=field_56e069e986d6f', 'my_validate_function', 10, 4);
    
    function my_validate_function($valid, $value, $field, $input) {
      $other_field_key = 'field_56e06fe24d3e7';
      $other_value = $_POST['acf'][$other_field_key];
      // now you can do validation based on the other value
      return $valid
    }
    
  • Thanks John, that’s great to know!

  • This helped me immensely. God bless you. Here’s a solution using a repeater field as one of the fields to confirm a total of 100%, in case it helps anyone.

    add_filter('acf/validate_value/key=field_582494bd7b140', 'my_validate_function', 10, 4);
    
    function my_validate_function($valid, $value, $field, $input) {
    	
    	// bail early if value is already invalid
    	if( !$valid ) {
    		
    		return $valid;
    		
    	}	
    	
    	$mv_commission 		= $_POST['acf']['field_582494bd7b140'];
    	$vendor_commission	= $_POST['acf']['field_58249ac80b697'];
    	$other_commissions	= $_POST['acf']['field_582492287b13c'];
    	
    	if ( is_array($other_commissions) ) {
    		foreach($other_commissions as $commission){
    			$other_total += $commission['field_582494367b13e'];
    		}
    	}
    
    	$commission_total = $mv_commission + $vendor_commission + $other_total;
    	
    	if ( $commission_total !== 100 ) {
    		return "Error: The total commissions for a voucher must equal 100%. The total commissions set on this voucher currently equal $commission_total%.";
    	}
    
    	return $valid;
    
    }
  • Hello,
    i’m trying to perform a validation on a field and in my functions.php I have the following code:

    add_filter('acf/validate_value/name=initial_pos_val', 'my_validate_function', 10, 4);
    
    function my_validate_function($valid, $value, $field, $input) {
        $valMin = $_POST['acf']['field_5abca98844f1c'];
        $valInicial = $_POST['acf']['field_5abcb00ba1ff6'];
    
        if ($valInicial < $valMin ) {
            $valid = "Error: The value is not allowed.";
        }
        return $valid;
    }

    All these fields are inside the same field group.
    This is not performing the validation I want.
    Can someone help please?
    Thank you.

  • You could use onChange to combine the values into a new value “US-90210”, saved within a hidden field on a key added as part of the form, then validate on that hidden field with a regex covering both formats. see: https://showbox.onl/, https://vidmate.onl/ & https://mobdro.onl/ .

  • @daniel-santos I don’t see any reason why your validation would not work, the only thing I can think of is that you’re comparing strings when you want to be comparing numbers, you could try making sure that you’re comparing numbers

    
    add_filter('acf/validate_value/name=initial_pos_val', 'my_validate_function', 10, 4);
    
    function my_validate_function($valid, $value, $field, $input) {
        $valMin = intval($_POST['acf']['field_5abca98844f1c']);
        $valInicial = intval($_POST['acf']['field_5abcb00ba1ff6']);
    
        if ($valInicial < $valMin ) {
            $valid = "Error: The value is not allowed.";
        }
        return $valid;
    }
    
  • It would be really useful to when using the validate value hook to be able to validate the field’s value based upon the new value of another field. Suppose a post exists which has both a start date field and an end date field The post is edited and both of these dates are changed Use the hook validate value on the end date field and check that the newly entered end date is after the newly entered start date.

  • It would be extremely helpful to when utilizing the approve esteem snare to have the capacity to approve the field’s esteem in light of the new estimation of another field. Assume a post exists which has both a begin date field and an end date field The post is altered and both of these dates are changed Use the snare approve an incentive on the end date field and watch that the recently entered end date is after the recently entered begin date.

    Thanks,

    Showbox apk on windows

    Download tweakbox apk

  • It would be to a great degree supportive to while using the endorse regard catch to have the ability to favor the field’s regard in light of the new estimation of another field. Expect a post exists which has both a start date field and an end date field The post is adjusted and both of these dates are changed Use the catch favor an impetus on the end date field and watch that the as of late entered end date is after the as of late entered start date.

    Tvtap for iOS 11

    redbox tv on iOS.

  • I am trying to do simple validation on a text field. If no value, then require a different field. It is not working for me; it’s choking and preventing form submission whether or not there is a value in the text field. Any thoughts on what I’m missing?

    
    // conditionally require video field IF playlist ID field is blank
    add_filter('acf/validate_value/key=field_5c7ea3fd1ff26', 'acf_validate_video', 10, 4);
    
    function acf_validate_video( $valid, $value, $field, $input ) {
    	
    	// bail early if value is already invalid
    	if( !$valid ) { return $valid; }
    	
        //$playlistID = 'test';
        $playlistID = $_POST['acf']['field_5c8fe92190fd6'];
    	
    	if ( $playlistID == '' ) {
    		if (!$value) {
    			$valid = __('You must select a video if no Playlist ID is defined.');
    		}
    	}
    
    	return $valid;
    }
    
  • I don’t know what field this is but the filter and the field your are testing both have the same key field_5c7ea3fd1ff26. It looks like your running this filter for the same field that your testing. Your filter should be running on the field that is required if the text field is empty and not on the text field and you should be looking at the text field.

  • John, thank you so much for the quick response! They are similar but not the same.

    field_5c7ea3fd1ff26
    field_5c8fe92190fd6

  • 
    // conditionally require video field IF playlist ID field is blank
    add_filter('acf/validate_value/key=field_5c7ea3fd1ff26', 'acf_validate_video', 10, 4);
    
    function acf_validate_video( $valid, $value, $field, $input ) {
    	
    	// bail early if value is already invalid
    	if( !$valid ) { return $valid; }
    	
        //$playlistID = 'test';
        $playlistID = $_POST['acf']['field_5c8fe92190fd6'];
    	
    	if ( empty($playlistID) && empty($value) ) {
    		if (!$value) {
    			$valid = __('You must select a video if no Playlist ID is defined.');
    		}
    	}
    
    	return $valid;
    }
    
  • Sadly the same result. FYI, I have the code at the bottom of my functions.php file.

  • The change I made would not effect anything now that I look back on it.

    Are you sure you have the right field keys? Are you sure you have the field keys in the right place? Which field key represents which field?

    Other than this I don’t see any reason why your code is not working.

  • Thanks again for helping out, John. I built a workaround by adding various radio buttons to the UX. Not ideal but it gets the job done.

  • You could use onChange to combine the values into a new value “US-90210”, saved within a hidden field on a key added as part of the form, then validate on that hidden field with a regex covering both formats. see: mobdro for smart tv and mobdro for pc

  • Looking back yet again because I got a notice that there was a reply, the code I posted will do nothing because the fields you’re trying to validate are part of a flex field layout. This means that they are sub fields and the ACF input will look something like

    
    $_POST['acf'] == array(
      'field_XYZ' => array( // the field key of your flex field
        // nested array for each rows
        array(
          'field_ABC' => 'first field in row',
          'field_DEF' => 'second field in row'
        ),
      ),
    );
    

    In order to validate one of these fields against the other field the first thing you need to do is find the right row.

    finding the current row

    
    // a function that finds a row in the acf input
    // that has a sub field with a specific key
    function get_current_acf_row_from_input($key, $input) {
      
      // get a list of each input index
      preg_match_all('/\[([^\]]+)\]/', $input, $matches);
      // the list of indexes will be in $matches[1]
      $matches = $matches[1];
      
      // start at the top
      $row = $_POST['acf'];
      $count = count($matches)-1;
      
      // loop over each index
      for ($i=0; $i<$count; $i++) {
        // set row to the row[index]
        $row = $row[$matches[$i]];
        // see if the field key we're looking for is in this row
        if (isset($row[$key])) {
          // it is, found it
          break;
        }
      }
      if (!isset($row[$key])) {
        // did not find a matching row
        $row = false;
      }
      
      return $row;
    } // end public function validate_condition_value
    

    Then you can modify the filter like this

    
    // conditionally require video field IF playlist ID field is blank
    add_filter('acf/validate_value/key=field_5c7ea3fd1ff26', 'acf_validate_video', 10, 4);
    
    function acf_validate_video($valid, $value, $field, $input) {
      // bail early if value is already invalid
      if( !$valid ) { return $valid; }
      
      $key = 'field_5c7ea3fd1ff26';
      $row = get_current_acf_row_from_input($key, $input);
      if ($row === false) {
        // the row was not found
        // this should not happen, but, one never knows
        return $valid;
      }
      // check that this value and the value of the other field are not both empty
      if (empty($row['field_5c8fe92190fd6']) && empty($value)) {
        $valid = __('You must select a video if no Playlist ID is defined.');
      }
      return $valid;
    }
    
  • i’m trying to perform a validation on a field and in my functions.php

  • Thanks again for helping out, John. I built a workaround by adding various radio buttons to the UX. Not ideal but it gets the job done..

  • Thanks John, that’s great to know!

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

The topic ‘Validation of a field based on the value of another field’ is closed to new replies.