List Field Empty Placeholder

Adds a placeholder value (default: ‘N/A’) to any empty List field cells during form submission. This ensures that empty list field values display consistently instead of being blank.

Instructions

Code

Filename: gw-list-field-empty-placeholder.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
<?php
/**
 * Gravity Wiz // Gravity Forms // List Field Empty Placeholder
 * https://gravitywiz.com/
 *
 * Instruction Video: https://www.loom.com/share/31db165c2a604a8b8573247c9416de2f
 *
 * Adds a placeholder value (default: 'N/A') to any empty List field cells during form submission.
 * This ensures that empty list field values display consistently instead of being blank.
 */
// Update "27" to your form ID.
add_filter( 'gform_pre_submission_filter_27', function ( $form ) {

	$field_ids       = array( 1 );    // List field IDs
	$column_numbers  = array( 3, 4 ); // Column numbers to apply logic to (1-based). Use empty array() for all columns
	$placeholder_val = 'N/A';         // Change placeholder text if needed

	foreach ( $form['fields'] as &$field ) {

		if ( ! in_array( (int) $field->id, $field_ids, true ) || $field->type != 'list' ) {
			continue;
		}

		$input_name = 'input_' . $field->id;

		if ( empty( $_POST[ $input_name ] ) || ! is_array( $_POST[ $input_name ] ) ) {
			continue;
		}

		$columns_count = count( $field->choices );
		foreach ( $_POST[ $input_name ] as $index => $value ) {

			if ( trim( (string) $value ) === '' ) {
				$column_index = $index % $columns_count;
				// Check if current column is in the target columns array, or apply to all if array is empty.
				if ( empty( $column_numbers ) || in_array( $column_index + 1, $column_numbers, true ) ) {
					$_POST[ $input_name ][ $index ] = $placeholder_val;
				}
			}
		}
	}

	return $form;
} );

Leave a Reply

Your email address will not be published. Required fields are marked *

  • Trouble installing this snippet? See our troubleshooting tips.
  • Need to include code? Create a gist and link to it in your comment.
  • Reporting a bug? Provide a URL where this issue can be recreated.

By commenting, I understand that I may receive emails related to Gravity Wiz and can unsubscribe at any time.