Making Repeater Field (or any of the fields inside it) required

I am trying to make a repeater field be required for a form, or even the fields inside it required, but I cannot make this happen.

  • I tried checking the “Required” checkbox but it becomes unchecked when saving the form

  • I tried setting the “required” attribute for each field to “true” but that does not work

  • I tried giving the repeater itself a “required” attribute of “true” and no luck there

Is there some way I can make this field or its fields required? It’s a requirement for the form.

Here is the code I have now in functions.php

add_filter( 'gform_form_post_get_meta_5', 'add_my_field' );
function add_my_field( $form ) {
    // Create a Single Line text field for the product member's name
    $purchasedFrom = GF_Fields::create( array(
        'type'   => 'text',
        'id'     => 1002, // The Field ID must be unique on the form
        'formId' => $form['id'],
        'required' => true,
        'label'  => 'Purchased From',
        'pageNumber'  => 1, // Ensure this is correct
    ) );
    $itemtype = GF_Fields::create( array(
        'type'   => 'text',
        'id'     => 1007, // The Field ID must be unique on the form
        'formId' => $form['id'],
        'required' => true,
        'label'  => 'Item Type',
        'pageNumber'  => 1, // Ensure this is correct
    ) );
    // Create an email field for the product s
    $quantity = GF_Fields::create( array(
        'type'   => 'text',
        'id'     => 1001, // The Field ID must be unique on the form
        'formId' => $form['id'],
        'required' => true,
        'label'  => 'Quantity',
        'pageNumber'  => 1, // Ensure this is correct
    ) );
    $purchasedDate = GF_Fields::create( array(
        'type'   => 'text',
        'id'     => 1003, // The Field ID must be unique on the form
        'formId' => $form['id'],
        'label'  => 'Date of Purchase',
        'required' => true,
        'pageNumber'  => 1, // Ensure this is correct
    ) );
    $serviceDate = GF_Fields::create( array(
        'type'   => 'text',
        'id'     => 1004, // The Field ID must be unique on the form
        'required' => true,
        'formId' => $form['id'],
        'label'  => 'Date Out of Service',
        'pageNumber'  => 1, // Ensure this is correct
    ) );
    $size = GF_Fields::create( array(
        'type'   => 'text',
        'required' => true,
        'id'     => 1009, // The Field ID must be unique on the form
        'formId' => $form['id'],
        'label'  =>'Size',
        'pageNumber'  => 1, // Ensure this is correct
    ) );
    $upc = GF_Fields::create( array(
        'type'   => 'text',
        'required' => true,
        'id'     => 1010, // The Field ID must be unique on the form
        'formId' => $form['id'],
        'label'  => 'UPC/Part # (From Receipt)',
        'pageNumber'  => 1, // Ensure this is correct
    ) );
    $damage = GF_Fields::create( array(
        'type'   => 'text',
        'required' => true,
        'id'     => 1005, // The Field ID must be unique on the form
        'formId' => $form['id'],
        'label'  => 'Description of Damage',
        'pageNumber'  => 1, // Ensure this is correct
    ) );


    // Create a repeater for the product and add the name and email fields as the fields to display inside the repeater.
    $product = GF_Fields::create( array(
        'type'             => 'repeater',
        'required'          => true,
        'id'               => 1000, // The Field ID must be unique on the form
        'formId'           => $form['id'],
        'label'            => 'Add Products',
        'addButtonText'    => 'Add Another Product',
        'removeButtonText'=> 'Remove Product',
        'pageNumber'       => 1, // Ensure this is correct
        'fields'           => array( $purchasedFrom,$itemtype, $quantity, $purchasedDate,$serviceDate, $size, $upc, $damage), // Add the fields here.
    ) );

    $form['fields'][] = $product;

    return $form;
}

// Remove the field before the form is saved. Adjust your form ID
add_filter( 'gform_form_update_meta_5', 'remove_my_field', 10, 3 );
function remove_my_field( $form_meta, $form_id, $meta_name ) {

    if ( $meta_name == 'display_meta' ) {
        // Remove the Repeater field: ID 1000
        $form_meta['fields'] = wp_list_filter( $form_meta['fields'], array( 'id' => 1000 ), 'NOT' );
    }

    return $form_meta;
}

Thanks!

The correct property to use when defining the field in code would be isRequired.

© 2008 - 2019. Gravity Forms is a project by Rocketgenius Inc.