Make fields inside a repeater conditionally required

Hello,
I’m building a multi page form consisting of 4 pages, for a “Work with us” page.
In the third page there is a repeater group of fields, which I built following the example in https://docs.gravityforms.com/repeater-fields/#h-example-2.
The fields are relative to someone’s work experience (start and end date, role, company) and they are all set as required.

Before those field there’s a yes/no radio button (first work experience?), default “no”.

If the value is “yes” I want the repeater fields to be NOT required and hide them, as they are not needed if the user didn’t work previously.

I used the gform_pre_render and gform_pre_validation filters following the example in https://docs.gravityforms.com/gform_pre_validation/#1-conditionally-required-field to make the field NOT required but either they are being validated when submitting the first page or they are NOT being validated when submitting the third page, based on how I tried to change the code.

I want them to be validated only when submitting the third page AND required if the radio button is set to “no”.

Any suggestion or example would be VERY appreciated, thank you.

To conditionally make the repeater fields required or not based on the value of the radio button, you can use the following code snippet:

add_filter( 'gform_pre_render', 'set_repeater_field_conditional_logic' );
add_filter( 'gform_pre_validation', 'set_repeater_field_conditional_logic' );

function set_repeater_field_conditional_logic( $form ) {
  foreach ( $form['fields'] as &$field ) {
    if ( $field->type != 'repeater' ) {
      continue;
    }

    // Check the value of the radio button field
    $first_work_experience = rgpost( 'input_X' ); // X is the ID of the radio button field

    if ( $first_work_experience == 'yes' ) {
      // Set the repeater field as not required
      $field->isRequired = false;
    } else {
      // Set the repeater field as required
      $field->isRequired = true;
    }
  }

  return $form;
}

This code uses the gform_pre_render and gform_pre_validation filters to check the value of the radio button field and set the repeater fields as required or not based on that value. Make sure to replace X in the code with the actual ID of your radio button field.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.