Support

Account

Home Forums General Issues Using acf/validate_value to validate repeater field

Solving

Using acf/validate_value to validate repeater field

  • I want to use acf/validate_value to make sure that a repeater field has at least one entry with its value set. However, the documentation doesn’t provide examples for the repeater field. How can I proceed ?

  • Hi @jpcote

    You can make use of the acf/validate_value filter to validate the value of the sub_fields within the repeater by hooking in to the filter using the $field_type.

    You can try something like this:

    add_filter('acf/validate_value/type={$field_type}', '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;
    		
    	}
    }
  • although this is old, for those who look for a good solution for this issue of validating the first row’s fields in a repeater, here is what I just figured out:

    //validate first step
    add_filter('acf/validate_value/key=field_57bc841e7f973', 'itamar_acf_validate_value__fs', 10, 4);
    function itamar_acf_validate_value__fs( $valid, $value, $field, $input ){
    	
    	// bail early if value is already invalid
    	if( !$valid ) {
    		return $valid;
    	}
    
     	// load data
    	$repeater_field = $_POST['acf']['field_57bc841e7f973']; //repater parent key
    	reset($repeater_field); //reset repeater array to the rist row
    	$first_field = key($repeater_field); //get first key in the repeater array
    	$my_text_field = $_POST['acf']['field_57bc841e7f973'][$first_field]['field_57bc8a5447d06']; //specific field
    	$characters = mb_strlen($my_text_field, 'utf-8');
    	if ( $characters < 100 ) {
    		$valid = 'כתבת '.$characters.' תווים בלבד בתוכן השלב הראשון. עליך לכתוב לפחות 100 תווים.';
    	} 
    		
    	// return
    	return $valid; 
    }

    in this case the validation is for a specific field under the first row in a specific repeater. you can change the characters count to 1 and then you get a real “required” field.

  • Thanks for sharing your code.

    I may be mistaken, but I believe that I can add a simplification to ituk’s post. If you just want the first item in the repeater, you can target it with the [0] array index without needing to reset the repeater field. So the following chunk of ituk’s code:

    
    $repeater_field = $_POST['acf']['field_57bc841e7f973']; //repater parent key
    reset($repeater_field); //reset repeater array to the rist row
    $first_field = key($repeater_field); //get first key in the repeater array
    $my_text_field = $_POST['acf']['field_57bc841e7f973'][$first_field]['field_57bc8a5447d06']; //specific field
    

    can be simplified to this:

    
    $my_text_field = $_POST['acf']['field_57bc841e7f973'][0]['field_57bc8a5447d06']; //specific field
    

    NOTE: This only works for the 1st item! It seems that the first item uses the [0] array index but each repeater subfield that gets added gets a randomly generated array index. So instead of going to [1] for the second item it will be something like [58b641f67f3de], which is a randomly generated string that is probably generated from the server’s timestamp.

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

The topic ‘Using acf/validate_value to validate repeater field’ is closed to new replies.