Support

Account

Home Forums General Issues Dont save post if sum of all repeater-fields are more than 100?

Solved

Dont save post if sum of all repeater-fields are more than 100?

  • Hey,

    I have a repeater with images. There is a subfield for the size. The size could be selected from a list like: 25, 50, 75…
    How can I check if the sum of all this subfields is not more than 100(%)?

    Thanks!

  • You need to and an acf/validate_value filter to the sub field, use the field key variant.

    
    // field_XXXXXXX == your sub field key
    add_filter('acf/validate_value/key=field_XXXXXXX', 'validate_repeater_sum', 20, 4);
    function validate_repeater_sum($valid, $value, $field, $input_name) {
      // field_YYYYYYY == your repeater field key
      $repeater = $_POST['acf']['field_YYYYYYY'];
      $sum = 0;
      foreach ($repeater as $row) {
        $sum += intval($row['field_XXXXXXX']);
      }
      if ($sum > 100) {
        $valid = 'The sum of all '.$field['label'].' fields must not exceed 100';
      }
      return $valid;
    }
    
  • thank you so much for your outstanding support!

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

You must be logged in to reply to this topic.