Using Gravity Forms ‘gform_trim_input_value’ PHP filter

The gform_trim_input_value Gravity Forms PHP filter allows you to disable the default behavior of trimming leading and trailing blank spaces from submitted form fields.

Usage

add_filter('gform_trim_input_value', 'disable_trim', 10, 3);

Parameters

  • $do_trim (boolean): The value being filtered. True to trim the value of the current field. False to disable trimming.
  • $form_id (integer): The ID of the current form.
  • $field (Field Object): The current field.

More information

See Gravity Forms Docs: gform_trim_input_value

Examples

Disable trimming for a specific field of a form

Disable trimming for field with ID ‘1’ in form with ID ’36’.

add_filter('gform_trim_input_value', 'disable_trim', 10, 3);

function disable_trim($do_trim, $form_id, $field) {
    if ($form_id == 36 && $field->id == '1') {
        return false;
    }
    return $do_trim;
}

Disable trimming for all fields of a specific form

Disable trimming for all fields in form with ID ’36’.

add_filter('gform_trim_input_value', 'disable_all_trim', 10, 3);

function disable_all_trim($do_trim, $form_id, $field) {
    if ($form_id == 36) {
        return false;
    }
    return $do_trim;
}

Disable trimming for a specific field type

Disable trimming for all fields of type ‘text’.

add_filter('gform_trim_input_value', 'disable_text_trim', 10, 3);

function disable_text_trim($do_trim, $form_id, $field) {
    if ($field->type == 'text') {
        return false;
    }
    return $do_trim;
}

Disable trimming for fields with specific input values

Disable trimming for fields with value ‘No trim’.

add_filter('gform_trim_input_value', 'disable_no_trim_value', 10, 3);

function disable_no_trim_value($do_trim, $form_id, $field) {
    if ($field->value == 'No trim') {
        return false;
    }
    return $do_trim;
}

Disable trimming for all fields

Disable trimming for all fields in all forms.

add_filter('gform_trim_input_value', 'disable_trim_everywhere', 10, 3);

function disable_trim_everywhere($do_trim, $form_id, $field) {
    return false;
}