Hey, I’ve been trying to add more in depth field validation to Gravity Forms using the following articles:
[Using the gform\_validation hook](https://docs.gravityforms.com/using-gform-validation-hook/) [g\_form validation](https://docs.gravityforms.com/gform_validation/?utm_term=&utm_campaign=Gravity+Forms+Video+2024&utm_source=adwords&utm_medium=ppc&hsa_acc=7958212457&hsa_cam=20934085623&hsa_grp=&hsa_ad=&hsa_src=x&hsa_tgt=&hsa_kw=&hsa_mt=&hsa_net=adwords&hsa_ver=3&gad_source=1&gclid=Cj0KCQiA84CvBhCaARIsAMkAvkKWOIN9F9WKIfC1Zpo1GkaG_megjwWsoOyh_jPyqICgNONyvaLGvjgaAkMbEALw_wcB)and
[gform\_field\_validation](https://docs.gravityforms.com/gform_field_validation/)all from the official Gravity Forms site, but either they’re outdated or I am not doing something right.
​
This is the PHP I have written so far in the functions.php file, I am simply trying to ensure a name field only contains letters and whitespace (default gravity forms allows numbers and special characters to be entered):
https://preview.redd.it/vumdniggm0oc1.png?width=726&format=png&auto=webp&s=7c0d36818cdeede2a2f15a9933bd96f980a6c07a
I get a billion errors when I try to submit to the form. One saying that not enough arguments are passed to validateName(), but I just followed what the article told me to do so I don’t understand what is being passed to validateName().
​
Anyone have better resources for figuring this out or knows how to add form validation to a gravity form?
[ad_2]
Is your form ID actually 9?
As per the documentation: *”Note: You’ll notice we’ve appended a ‘9’ to the end of the ‘gform_validation’ filter name. This means that we only want to trigger our custom validation function from form ID 9. No sense in running an extra function on every form submission if we only need it on one specific form. Just replace the 9 with the ID of the form you’d like to target.”*
I changed my PHP to the following:
add_filter(‘gform_validation_9’, ‘validateName’, 10, 4);
function validateName($result, $value, $form, $field){
if($result[‘is_valid’] && $field->cssClass == ‘validate-name’){
if(ctype_alpha(str_replace(‘ ‘, ”, $value)) === false){
$result[‘is_valid’] = false;
$result[‘message’] = “Name fields must contain letters and spaces only”;
}
}
return $result;
}
and it now allows the form to submit without errors, **the form is still accepting invalid data (names with numbers) but at least no more errors?**
What stopped the errors was providing two more arguments to the add_filter() method, the 10 and 4. In the examples there are either only 2 arguments there, or in the ones with four arguments it has a 10 and 2 instead of a 10 and 4. It was [this ](https://community.gravityforms.com/t/what-are-the-two-numbers-in-the-filter-examples/4215)[forum](https://community.gravityforms.com/t/what-are-the-two-numbers-in-the-filter-examples/4215) that made me think to change the 2 to a 4… **still confusing though since the examples use 2 despite the filter having 4 arguments.**