Support

Account

Home Forums Add-ons Repeater Field Use of repeater field with conditional logic Reply To: Use of repeater field with conditional logic

  • I think the main issues is a misunderstanding for a lot of people that have this problem. Conditional login only controls what is visible in the admin.

    1) You create a field like a radio field
    2) You set logic on other fields to display or not display a field based on the other field.

    This does not control what is saved in any of the fields you set up in #2 nor will it alter what has already been saved.

    It cannot be assumed that because a field has a value that the radio field it is conditional on is set a specific way, this is backwards logic. You need to check how the radio field is set and then proceed to show the field that is dependent on the choice made.

    Example:

    
    if (have_rows('repeater')) {
      ?>
        <ul>
          <?php 
            while (have_rows('repeater')) {
              the_row();
              ?>
                <li>
                  <?php 
                    $content_type = get_sub_field('content_type');
                    switch ($content_type) {
                      case 'video':
                        the_sub_field('video');
                        break;
                      case 'text':
                        the_sub_field('text');
                        break;
                      case 'image':
                        the_sub_field('image');
                        break;
                      case 'ect....':
                        // something else
                        break;
                    } // end switch
                  ?>
                </li>
              <?php 
            } // end while have_rows
          ?>
        </ul>
      <?php 
    } // end if have_rows
    

    the switch above could also be written using multiple if’s, but I like switches.

    
    if ($content_type == 'video') {
      the_sub_field('video');
    } elseif ($content_type == 'text') {
      the_sub_field('text');
    } elseif (.... etc...
    

    As far as added previously chosen content into a new row. That is harder to explain.

    Let’s say that I have 2 rows, the first is set to an image and the second is set to text. I then add a 3rd row, but I place it 2nd in the list and I make this new row an video field.

    before:
    1) image
    2) text

    after
    1) image
    2) video
    3) text

    In this case, the second row will retain the original text value from before the update. Because the second row was changed to “video” the “text” field associated with this row is hidden and not submitted. Since it is not submitted it is not updated. The main reason for this is data validation. ACF does not look at the conditional logic when a submission is made. It will not look at, for example, the text field, and check the conditional logic on that row to see if text was selected. Data validation is only concerned with the value of the field being submitted. ACF bypasses validation on the field by not submitting the value. This is what makes it possible to not set a value in a required field when that field is hidden by conditional logic.