Set a minimum number of rows for a list field

Hi, see there’s a few bits and pieces that come up when doing a Google search, but all old and not really a solution. Our form field question is asking for the names of co-freeholders for a block of flats, but often only one person’s name is supplied. There should be at least 2, so hoping can set the minimum number of rows required to 2.

Thanks

The filter gform_field_value_$parameter_name can be used to set an initial number of rows that are set for a list. For a single column list, the following would suffice…

add_filter( 'gform_field_value_list_two_rows', function( $value ) {

   return [
      array(),
      array()
   ];

} );

Reference that filter documentation on usage and how to enable dynamic population on a list field.

The :heavy_plus_sign:/:heavy_minus_sign: icons could be removed by targeting them in CSS so that users can’t remove those first two rows from the list.

You can then add some validation via gform_field_validation (see 17. List field validation as a starging point) to ensure that the first two rows contain values as expected.

1 Like

Thanks Joshua,

Please forgive my ignorance, as self-teach here. I posted 17. List field validation into snippets functions.php, changing the form ID and field number. Please let me know what validate_list_field' `, 10, 4 ); means?

Then I pasted your code above into snippets first, then to form settings, custom Javascript and on both occasions nothing happened. In fact it disabled some of the code already there. Clearly I haven’t done it right, and would really welcome any assistance.

Thanks
Steve

The code provided is PHP code. As such, you’ll need to add it via functions.php or a PHP code snippets plugin – sounds like the fist place you had placed it. The validation function will only run upon form submission, ensuring all cells of the list field are filled before submission is allowed.

In order for the small snippet I provided above to have any effect (i.e. default the list to two rows from the get-go), you will need to not only add the code to a snippet, but also set list_two_rows as the value for List → Field Settings → Advanced → Allow field to be populated dynamically → Parameter Name.

1 Like

Thanks, so under snippets php I added the following:

// Require all inputs for a list field.
  
add_filter( 'gform_field_validation_8_99', 'validate_list_field', 10, 4 );
function validate_list_field( $result, $value, $form, $field ) {
    if ( $field->type == 'list' ) {
  
        GFCommon::log_debug( __METHOD__ . '(): List Field: ' . print_r( $value, true ) );
  
        foreach ( $value as $row_values ) {
        GFCommon::log_debug( __METHOD__ . '(): Row Value: ' . print_r( $row_values, true ) );
  
            $column_1 = rgar( $row_values, 'Type' );
            GFCommon::log_debug( __METHOD__ . '(): Column 1: ' . print_r( $column_1, true ) );
  
            $column_2 = rgar( $row_values, 'Cost' );
            GFCommon::log_debug( __METHOD__ . '(): Column 2: ' . print_r( $column_2, true ) );
  
            $column_3 = rgar( $row_values, 'Frequency' );
            GFCommon::log_debug( __METHOD__ . '(): Column 3: ' . print_r( $column_3, true ) );
  
            if ( empty( $column_1 ) || empty( $column_2 ) || empty( $column_3 ) ) {
                $has_empty_input = true;
            }
        }
  
        if ( $has_empty_input ) {
            $result['is_valid'] = false;
            $result['message']  = 'All inputs are required!';
        }
    }
  
    return $result;

Your code in your first message wasn’t accepted in snippets.
I added in parameter name list_two_rows.

It’s not working, down to me and being a novice surely not doing it right. Joshua thank you for your patience, hoping you can let me where I continue to go wrong?

Thanks
Steve

Try adding the code from the original message again. I updated, as there was a syntax error. Should be accepted now.

Although, if you have a multi-column list field, it may still need slightly tweaked.

Thanks for your time Joshua.
I added the following to snippets php:
add_filter( ‘gform_field_value_list_two_rows’, function( $value ) {

return [
array(),
array()
];

} );

Under the field list, parameter name, added: list_two_rows

This is a 3 column list field.

It’s not working, so can I please ask for a little more assistance, and no doubt patience, as know can’t be easy dealing with queries from people like me with very little know-how.

Thanks
Steve

This code is working for me locally. Here’s what I see when first rendering the form when the code is in place…

Two things to check.

  1. Ensure the single-quotes around the filter name are basic single-quotes 'gform_field_value_list_two_rows' and not curly quote ‘gform_field_value_list_two_rows’. Your reply above seems to have them copied as curly.
  2. If using the code snippets plugin, make sure the snippet is set to run across the whole site and not only in the admin.

Beyond that you would probably need to add some logging statements to the snippet to first ensure the code is indeed being run or may want to connect with someone that can log in to the site and investigate further.

Dear Joshua,

Thanks, to re-cap I’ve only done the following, no curly quotes, snippets, allow everywhere:
add_filter( ‘gform_field_value_list_two_rows’, function( $value ) {

return [
array(),
array()
];

} );

Field applying to, advanced, Parameter name: list_two_rows

Not done anything else and despite your kind help it isn’t working. Finally if there’s something else I need to do please advise?

Regards
Steve

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