How to add custom validation for a Gravity Forms field

This is kind of a follow-up post to my previous post where I showed how to restrict a text area field in a Gravity Form to a certain number of words. That post used only JavaScript to limit the number of words which is not the most fool-proof way to do it since a user can just disable the javascript and still submit their content successfully.

This post will use PHP code to check the same text area field and produce an error on the form submission. Since we’re adding the check on form submission, it will be impossible for a user to bypass it even with the JavaScript disabled. I highly recommend using a child theme or a custom plugin to write the PHP code but if you’re not a developer or just starting out, you can use this plugin (Code Snippets) to add some PHP code to your WordPress website.

As we did in the previous post, add a text area field to your Gravity Form and note down the field id of the field you just added. You can take a look at the field id this way. As you can see, my field id is 10. We’re going to use this field id to add our custom validation. Gravity Forms by default adds basic validation but we’re extending it a bit further.

Right now, if you disable JavaScript in your browser, you will be able to submit the form without any errors and it will let you submit as many words as you can. Here’s how to test it by disabling JavaScript in Google Chrome and test the form submission.

As you can see, we were able to submit as many words as we want. Now, add the following PHP code using the Code Snippets plugin or your preferred method of adding custom code. I am going to explain what it does.

//The following declaration targets field 10 in form 1
add_filter( 'gform_field_validation_1_10', 'field_10_validation', 10, 4 );
function field_10_validation($result, $value, $form, $field){
  $splitstring = explode(" ",$value);
  if($result['is_valid'] && count($splitstring) > 10){
    $result['is_valid'] = false;
    $result['message'] = "Please make sure there are 10 or less words in field 4";
  }
  return $result;
}

Here’s how to add the code using the code snippets plugin. Once you add and enable it, it will start validating our text area field on form submission. You can limit the application of this code on the front end only if you’d like.

You can read more about the Gravity Form filter I’m using here. This page also has examples of how to use the same filter for other types of fields like address field validation in a Gravity Form. The first number after gform_field_validation_ is your form no and the next number is the field you’re targeting. It’s better to include both of them so that you know what precise field you’re targeting.

This filter passes four arguments as you can see which are $result, $value, $form, $field and you can modify values of each of these parameters based on what you want to do. It’s very important to return $result at the end so that it doesn’t break Gravity Form code execution.

The “explode” in our second line, breaks the string entered by the user using the space character and stores the results in a PHP array. We then count the total number of elements in the array and mark the result as invalid using the “$result[‘is_valid’]=false” line. The “$result[‘message’]” allows you to add your own custom error message on validation fail. Here’s how the end result looks even if we disable JavaScript. Happy coding.

Leave a Comment