Validate list fields

Hi,

In my form, I have a list with fields in the first column being dropdowns.

11

I want to add validation to the list

  • a field in each row needs to be filled in
  • fields in the column can be numbers only, up to 100, no negative numbers

What’s the best way to do it?

The best way is to use the gform_field_validation filter:

When you say " * a field in each row needs to be filled in" you can accomplish that by making the field required in the form builder. If you need to require ALL fields in a row, you can use this solution:

https://gravitywiz.com/require-all-columns-of-list-field/

1 Like

Hi Chris,

Thank you for the links. I have tried using gform_field_validation filter but without success. Unfortunately, there is no example of the page of coding custom validation of the multicolumn field.

I have fried, a few different approaches, but none of them worked. Here are some examples.

add_filter( ‘gform_field_validation_7_36’, ‘mm_list_custom_validation_1’, 10, 4 );
function mm_list_custom_validation_1( $result, $value, $form, $field ) {

$val = rgar( $value, $field->id . '.2' );

if ( $val > 10 ) {
    $result['is_valid'] = false;
    $result['message'] = 'Please enter a value less than 10';
}
return $result;

}

add_filter( ‘gform_field_validation_7_36’, ‘mm_list_custom_validation_2’, 10, 4 );
function mm_list_custom_validation_2( $result, $value, $form, $field ) {

if ( class_exists( 'gfield_list_36_cell2' ) && $val > 10 ) {
    $result['is_valid'] = false;
    $result['message'] = 'Please enter a value less than 10';
}
return $result;

}

add_filter( ‘gform_field_validation_7_36’, ‘mm_list_custom_validation_3’, 10, 4 );
function mm_list_custom_validation_3( $result, $value, $form, $field ) {

if ( $field->get_input_type() == 'text' && $val > 10 ) {
    $result['is_valid'] = false;
    $result['message'] = 'Please enter a value less than 10';
}
return $result;

}

add_filter( ‘gform_field_validation_7_36’, ‘mm_list_custom_validation_4’, 10, 4 );
function mm_list_custom_validation_4( $result, $value, $form, $field ) {

if ( $field->get_input_type() == 'text' && $val > 10 ) {
    $result['is_valid'] = false;
    $result['message'] = 'Please enter a value less than 10';
}
return $result;

}

Here is an output of the list field:
[36] => a:9:{i:0;a:2:{s:14:“Fuel component”;s:4:“c2h6”;s:5:“% Mol”;s:2:“11”;}i:1;a:2:{s:14:“Fuel component”;s:6:“ic4h10”;s:5:“% Mol”;s:2:“13”;}i:2;a:2:{s:14:“Fuel component”;s:5:“c5h12”;s:5:“% Mol”;s:1:“2”;}i:3;a:2:{s:14:“Fuel component”;s:5:“c7h16”;s:5:“% Mol”;s:1:“5”;}i:4;a:2:{s:14:“Fuel component”;s:2:“h2”;s:5:“% Mol”;s:2:“15”;}i:5;a:2:{s:14:“Fuel component”;s:2:“co”;s:5:“% Mol”;s:1:“9”;}i:6;a:2:{s:14:“Fuel component”;s:2:“n2”;s:5:“% Mol”;s:1:“9”;}i:7;a:2:{s:14:“Fuel component”;s:3:“co2”;s:5:“% Mol”;s:1:“2”;}i:8;a:2:{s:14:“Fuel component”;s:3:“h2o”;s:5:“% Mol”;s:2:“34”;}}

Here is how a screenshot of how the form looks:

Any feedback would be greatly appreciated.

I put together a custom validation for a multiple column list field that checks if all fields in all columns provided by a user value and checks a few other things that I described below.

Here is the code that I use. Most of it is comments describing what each section does.

Hopefully, it will make it easier for others to add custom validation to their list filed.

Also, I strongly recommend the plugin author adding this or a similar example to the https://docs.gravityforms.com/gform_field_validation/ page as it is missing code example for validating a list field. Thank you.

/**
 * Custom list validation for form 7, field ID 36
 * Helpful resources:
 * - About Validation: https://docs.gravityforms.com/gform_field_validation/
 * - About list field: https://docs.gravityforms.com/gf_field_list/
 */
add_filter( 'gform_field_validation_7_36', 'mm_list_custom_validation', 10, 4 );
function mm_list_custom_validation( $result, $value, $form, $field ) {

    foreach ($value as $row => $array) {

    /* Exmple of $value output for list 36 in form 7:
    (
        [0] => Array
            (
                [Fuel component] => c2h6
                [%] => 101
            )
        [1] => Array
            (
                [Fuel component] =>
                [%] =>
            )
        [2] => Array
            (
                [Fuel component] => ic4h10
                [%] => 11
            )
        [3] => Array
            (
                [Fuel component] =>
                [%] => co2
            )
        [4] => Array
            (
                [Fuel component] =>
                [%] => 313
            )
    )
    */

    /* Example of $array variable for list field 36 in form 7
        (
            [Fuel component] => c2h6
            [%] => 101
        )
    */

        $sum += $array['%'];

        // If the list field is set to required in the Gravity Form form editor, perform action in the curly brackets
        if ( $field['isRequired'] = 1 ) {
            // triger an error and show a custom error message on the page when any field in any of columns is empty
            foreach ($array as $key => $val) {
                if ($val == '') {
                    $result['is_valid'] = false;
                    $result['message'] = "Please fill out all input fields.";
                }
            }
        }

        // triger an error and show a custom error message on the page when one of values in the column with name '%' is more than 100
        if ($array['%'] > 100) {
            $result['is_valid'] = false;
            $result['message'] = 'Please enter a value less than or equal 100';
        }
    }

    // triger an error and show a custom error message on the page when the total of values in the column with name '%' is less than or more than 100
    if ($sum != 100) {
        $result['is_valid'] = false;
        $result['message'] = "The total of entered values is $sum. Please adjast values so that the total equals 100.";
    }

    return $result;
}

Here is the updated version of the above script https://github.com/MarcinKilarski/gravity-forms-snippets

Also, I wish there was a way to add an error class to a specific field with an invalid value instead of adding it to the whole list filed and turning all field red. It would be nice to only turn red fields with invalid values. If someone knows how to do it, please let me know. Thanks :slight_smile:

4 Likes

I rewrote this function for my needs which was to check a list field: Name, Email, and Phone Number.

I strongly recommend the plugin author adding this or a similar example to the https://docs.gravityforms.com/gform_field_validation/ page as it is missing a code example for validating list fields. Thank you.

The php function checks:

  1. All fields have a value
  2. Email is valid using FILTER_VALIDATE_EMAIL
  3. Phone Number is valid using FILTER_SANITIZE_NUMBER_INT

Note:
a. This code will go into your ‘functions.php’ file
b. With the filter ‘gform_field_validation_23_110’: 23 is the form-id and 110 is the field-id

add_filter( ‘gform_field_validation_23_110’, ‘validate_list_field’, 10, 4 );
function validate_list_field( $result, $value, $form, $field ) {
foreach ($value as $row => $array) {

	/* Check Value for each field: Name, Phone Number, and Email */
foreach ($array as $key => $val) {
        if ($val == '') {
            $result['is_valid'] = false;
            $result['message'] = "Please fill out all fields.<br>";
        }
    }       
    
    /* Validate Email */
    if ( !empty( $array['Email']) && (!filter_var($array['Email'], FILTER_VALIDATE_EMAIL)) ) {        	
        $result['is_valid'] = false;
        $result['message'] .= "Email Address not valid: " . $array['Email'] . "<br>";            
    }
    
    /* Validate Phone */
    $valid_number = filter_var($array['Phone Number'],FILTER_SANITIZE_NUMBER_INT);         
    if ( (strlen($valid_number) < 10) && (strlen($array['Phone Number']) > 0) ) {

        $result['is_valid'] = false;
        $result['message'] .= "Phone Number not valid: " . $array['Phone Number'] . "<br>";  
	}		
}	
return $result;

}

For entire code please see: