How to validate all fields

I’m trying to add validation to all forms and all fields but it’s not working. 1) It breaks the form redirect 2) It only works if I target a specific field on a specific form.

Here’s my code:

add_filter( 'gform_field_validation', 'validate_url', 10, 4 );
function validate_url( $result, $value, $form, $field ) {
    $nourl_pattern = '/(?i)(http|https).*/';
    $nourl_message = 'Message must not contain website addresses.';
    if ( preg_match( $nourl_pattern, $value ) ) {
        $result['is_valid'] = false;
        $result['message']  = $nourl_message;
    }
    return $result;
}

The regex pattern simply looks for http/https because we don’t want spammers inserting URLs into fields.

Hi Mark. It’s hard to say exactly what is happening with your code. I find that the best way to troubleshoot filter usage like this is by adding lots of custom logging statements.

First, enable logging:

Next, refer to this documentation for HOW to add the statements:

I recommend logging every variable or object right after it has a value, so you can make sure it contains what you think it should. You can also add statements inside loops or conditions to be sure your code was executed. Try something like this (after enabling logging):

<?php
add_filter( 'gform_field_validation', 'validate_url', 10, 4 );
function validate_url( $result, $value, $form, $field ) {
    GFCommon::log_debug( __METHOD__ . '(): The current value => ' . print_r( $value, true ) );
    GFCommon::log_debug( __METHOD__ . '(): The current field => ' . print_r( $field, true ) );
    GFCommon::log_debug( __METHOD__ . '(): The initial result => ' . print_r( $result, true ) );
    $nourl_pattern = '/(?i)(http|https).*/';
    $nourl_message = 'Message must not contain website addresses.';
    if ( preg_match( $nourl_pattern, $value ) ) {
        GFCommon::log_debug( __METHOD__ . '(): We have a match!' );
        $result['is_valid'] = false;
        $result['message']  = $nourl_message;
    }
    GFCommon::log_debug( __METHOD__ . '(): The modified result => ' . print_r( $result, true ) );
    return $result;
}

Hi Chris.

I was able to get the log. So I see this in the log:

2020-10-11 21:08:59.547555 - DEBUG --> validate_url(): The current value => https://theverge.com/ 
2020-10-11 21:08:59.547609 - DEBUG --> validate_url(): The initial result => Array
(
    [is_valid] => 1
    [message] => 
)
 
2020-10-11 21:08:59.547631 - DEBUG --> validate_url(): We have a match! 
2020-10-11 21:08:59.547655 - DEBUG --> validate_url(): The modified result => Array
(
    [is_valid] => 
    [message] => Message must not contain website addresses.
)
 
2020-10-11 21:08:59.547692 - DEBUG --> GFFormDisplay::process_form(): After validation. Is submission valid? No. 
2020-10-11 21:09:19.261701 - DEBUG --> GFFormDisplay::process_form(): Starting to process form (#3) submission. 

But then later in the log I see this:

2020-10-11 21:09:19.264503 - DEBUG --> GFFormDisplay::process_form(): After validation. Is submission valid? Yes. 
2020-10-11 21:09:19.264533 - DEBUG --> GFFormDisplay::process_form(): Submission is valid. Moving forward. 
2020-10-11 21:09:19.264588 - DEBUG --> GFFormsModel::save_entry(): Saving entry. 
2020-10-11 21:09:19.269015 - DEBUG --> GFFormsModel::save_entry(): Entry record created in the database. ID: 26005. 
2020-10-11 21:09:19.269434 - DEBUG --> GFFormsModel::save_entry(): Saving entry fields.

Can you try setting $result['is_valid'] = false;

to

$result['is_valid'] = 0;

[is_valid] => 

is empty when you find a match, so I think that is where the problem lies.

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