Support

Account

Home Forums Add-ons Repeater Field update_sub_field() not triggering validation

Solved

update_sub_field() not triggering validation

  • Hi there!

    I’m using a nested repeater inside a nested repeater => a list of companies containing a list of emails each. They are saved in an options page. I’m trying to update only the list of emails of a specific company via the frontend, which I managed to do with:

    if(have_rows('field_64d1e8c0387f6', 'falke_amsc_options')) {
        while(have_rows('field_64d1e8c0387f6', 'falke_amsc_options')) {
            the_row();
            $emails= get_sub_field('field_64d1e937387fa'); // 'emails'
            if([condition...]) {
                $counter = 0;
                while($_POST["email-$counter"]) {
                    $newEmails[] = array( 'field_64d1e947387fb' => $_POST["email-$counter"] );
                    $counter++;
                }
                update_sub_field( 'field_64d1e937387fa', $newEmails, 'falke_amsc_options' ); // 'emails'
            }
        }
    }
    // field_64d1e8c0387f6 => 'companies' repeater
    // field_64d1e937387fa => 'emails' repeater
    // field_64d1e947387fb => 'email' field

    Saving the new values works perfectly fine. However, my custom validation checking if any email isn’t unique isn’t triggered when saving that way. Updating the values in the backend triggers the validation normally. Not using update_sub_field() though. Even a dummy-validation doesn’t trigger:

    function falke_acf_validate_dummy( $valid, $value, $field, $input ) {
      // Bail early if value is already invalid.
      if( $valid !== true ) {
        return $valid;
      }
      return __( "Don't save the values" );
    } add_filter('acf/validate_value/key=field_64d1e947387fb', 'falke_acf_validate_dummy', 10, 4);
    // doesn't work either
    add_filter('acf/validate_value/key=field_64d1e937387fa', 'falke_acf_validate_dummy', 10, 4);

    I also tried adding the filter to the repeater containing the emails instead of the email-field itself, didn’t work either. Is there anything else I need to consider? How do I trigger the validation of my e-mail-fields when updating them with update_sub_field()?

    Thanks in advance!

  • ACF functions for updating fields does no trigger validation. Validation is only triggered when either the fields are submitted in the form in the admin or when using acf_form().

    Since you are using your own form with your own fields you would need to perform your own validation of the values in $_POST

  • Well, that’s a bummer. I thought it’d always trigger before writing anything to the database. Then I’ll have to write another validation for the frontend like you suggested. Thank you!

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

You must be logged in to reply to this topic.