Check a field in Gravity Forms against a list of allowed values

gf-field-validation

I worked on a project recently where the client wanted to allow users to sign up to his premium content site, but only if the users were members of companies with whom he had made agreements for them to be able to access his content. I suggested that he handle this by providing a code to each company. The company would then make the unique code available to its members, such as on a bulletin board,  in a letter to the employees or on the company’s private intranet.

In order to facilitate this unique sign-up process, I created a form using Gravity Forms which would allow users to register to the site, but included an extra required field called “Company Code”.

Using the gform_field_validation() filter, I could perform a check on the value entered into that field and only allow the form to complete submission if the code matched one of the ones in our array of valid codes.

The code (which belongs in your functionality plugin) converts the entered value to uppercase (in case users use lowercase letters instead) and then sees if it exists in the pre-defined array of codes and their associated companies.

If the code entered doesn’t exist in the array, the $result is modified so that ‘is_valid’ is set to false and a validation message is set.

gf-field-validation

Of course, you can create any sort of validation to suit your needs. For example, in this snippet, we’re maintaining the valid codes in the plugin but you could maintain that array from the dashboard either by creating an entry in wp_options from some sort of settings page or you could add them as a custom post type and search for valid codes that way. As ever with WordPress and Gravity Forms, the options are endless.

Get Gravity Forms

Leave a Reply