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.

Hi Chris,

I apologize for the late reply. Thats it! Solved!

Thank you so much!

Although, I would like to add that it breaks the redirect options I have in Wordpress dashboard.

Here is a log entry for when it works:

2020-11-03 19:13:19.403423 - DEBUG --> GFCommon::send_email(): Result from wp_mail(): 1 
2020-11-03 19:13:19.403539 - DEBUG --> GFCommon::send_email(): WordPress successfully passed the notification email (#5b19847edc75a - Admin Notification) for entry #26396 to the sending server. 
2020-11-03 19:13:19.410557 - DEBUG --> GFFormDisplay::handle_confirmation(): Sending confirmation. 
2020-11-03 19:13:19.410811 - DEBUG --> GFFormDisplay::handle_confirmation(): Confirmation => <script type="text/javascript"> function gformRedirect(){document.location.href="https:\/\/REDACTED-URL.com\/contact-submission\/?lp=ePrice";}</script> 
2020-11-03 19:13:19.410892 - DEBUG --> GFFormDisplay::process_form(): Executing functions hooked to gform_after_submission.

Here is a log entry when I use the URL filter. For some reason it does not execute the URL redirect.

2020-11-03 19:19:01.408597 - DEBUG --> GFFormDisplay::process_form(): Starting to process form (#63) submission. 
2020-11-03 19:19:01.410202 - DEBUG --> GFFormDisplay::process_form(): Source page number: 1. Target page number: 0. 
2020-11-03 19:19:01.411087 - DEBUG --> GFFormDisplay::process_form(): After validation. Is submission valid? Yes. 
2020-11-03 19:19:01.411159 - DEBUG --> GFFormDisplay::process_form(): Submission is valid. Moving forward. 
2020-11-03 19:19:01.411259 - DEBUG --> GFFormsModel::save_entry(): Saving entry. 
2020-11-03 19:19:01.415607 - DEBUG --> GFFormsModel::save_entry(): Entry record created in the database. ID: 26476. 
2020-11-03 19:19:01.416338 - DEBUG --> GFFormsModel::save_entry(): Saving entry fields. 
2020-11-03 19:19:01.416521 - DEBUG --> GFFormsModel::queue_save_input_value(): Queued field operation: First Name:(#1 - text). 
2020-11-03 19:19:01.416611 - DEBUG --> GFFormsModel::queue_save_input_value(): Queued field operation: Last Name:(#2 - text). 
2020-11-03 19:19:01.416692 - DEBUG --> GFFormsModel::queue_save_input_value(): Queued field operation: Phone:(#4 - text). 
2020-11-03 19:19:01.416781 - DEBUG --> GFFormsModel::queue_save_input_value(): Queued field operation: Name of Coin:(#5 - text). 
2020-11-03 19:19:01.416865 - DEBUG --> GFFormsModel::queue_save_input_value(): Queued field operation: Additional Comments:(#6 - textarea). 
2020-11-03 19:19:01.416965 - DEBUG --> GFFormsModel::queue_save_input_value(): Queued field operation: UTM Campaign(#7 - hidden). 
2020-11-03 19:19:01.420497 - DEBUG --> GFFormsModel::save_entry(): Finished saving entry fields. 
2020-11-03 19:19:01.589648 - DEBUG --> GFCommon::is_spam_entry(): Result from Akismet: true 
2020-11-03 19:19:01.589731 - DEBUG --> GFCommon::is_spam_entry(): Is submission considered spam? Yes. 
2020-11-03 19:19:01.622129 - DEBUG --> GF_Background_Process::dispatch(): Running for gf_feed_processor. 
2020-11-03 19:19:01.637698 - DEBUG --> GF_Background_Process::dispatch(): Unable to dispatch tasks to Admin Ajax: Nothing left to process 
2020-11-03 19:19:01.637805 - DEBUG --> GFFormDisplay::handle_confirmation(): Sending confirmation. 
2020-11-03 19:19:01.638050 - DEBUG --> GFFormDisplay::handle_confirmation(): Confirmation => <div id='gf_63' class='gform_anchor' tabindex='-1'></div><div id='gform_confirmation_wrapper_63' class='gform_confirmation_wrapper '><div id='gform_confirmation_message_63' class='gform_confirmation_message_63 gform_confirmation_message'>Thanks for contacting us! We will get in touch with you shortly.</div></div> 
2020-11-03 19:19:01.638108 - DEBUG --> GFFormDisplay::process_form(): Executing functions hooked to gform_after_submission.