Posted on by

Custom Field Validation

Setting up a custom validation method is relatively easy if you’ve got some PHP skills. If you’re familiar with using WordPress filters, then you’ve got the basic knowledge you need to set this up.

If you’re not sure what WordPress filters are and how to use them, there is a good intro article that should give you the basics: WordPress Actions, Filters, and Hooks : A guide for non-developers

Generally, however, doing this is going to require some knowledge of how to write some basic code because you’re going to have to figure out how to fill your particular need. I’m giving you a very simple example so you can see how all the pieces work.

How to Build a Custom Validation

A custom validation method consists of two parts: the validation code and the validation error message. The validation code is the part that looks at the submitted value and decides whether the input is OK or not. The error message is the user feedback message that is shown when a particular type of validation error occurs.

We will need to create a callback for each of these two things. It’s best to put these in a simple plugin, but your theme functions file will do also.

The Validation Callback

This callback is set up on the ‘pdb-before_validate_field’ action. The way this works is the action is triggered just before each field validation occurs. If you set up a callback that validates the field, that field won’t get validated by the normal methods because the plugin will see that the field has already been validated.

If you refer to the docs, you’ll see the properties of the object that is passed in with this action. All we need to do in our callback is check the field to see if it is one that we are validating, then check the value to see if it is valid, then set the ‘error_type’ property to either ‘valid’ or, if it is invalid, the type of validation error it is. That validation error type is the key to a validation error message which we will define later.

An Example Validation Callback

Let’s say we’re doing something really simple like checking to make sure the value is a valid URL. If the user submits a valid URL we mark it “valid” but if it is not, we mark it “invalid url”. Here is the callback code for that:

Pretty simple, huh? Of course, a real-world example would be more complex, but you can basically do anything you want inside that function to validate the field. For instance, if you wanted to validate the field against the value of another field, you can do that by inspecting the $_POST array, which will contain all the values that are currently getting submitted.

Setting Up the Validation Error Message

This one requires a filter callback that adds your validation error message to the array of validation methods the plugin uses to give the user feedback after they submit a form.

As per the docs, the filter passes in an array of validation error messages. We just need to add our message to that array and return it so it can be shown to the user if the field validation fails. Here’s a simple example of how to do that:

And that’s all there is to that. In a real application, the error message would likely be wrapped in a gettext function so that it can be translated.

Now, all that’s left to do is test it to make sure it works the way you want.

11 thoughts on “Custom Field Validation

  1. Hi Roland, I have an issue when validating ordinary fields. When an error type field happens, the user is redirected to the posts page. Any idea why this could be happening?
    I read almost all the documentation trying to find any place where to set this but I couldn’t. I only want to show the same signup form with a simple message near the field, without being redirected anywhere.
    Thanks!

    1. That is very unusual behavior. Normally, of course, the user stays on the same page to see the error message and make any changes to the form.

      I can only guess there is some other plugin or your theme that is causing the redirect. If you want to post a link to the form, I’ll take a look at it.

      You may find that using client-side validation works better, you can do this by adding a “required” attribute to the fields…put this in the “values” field of your field definition: required::required

      1. Thanks! that worked !!
        One more question, what code do i put in the fields value of a dropdown menu to select an option by default? And also to make this field required.

        1. The field definition on the Manage Database Fields page has a “default” value you can fill in to set the preselected value. Also, this is where you can make the field required.

  2. Hi,

    Thank you for this great plugin. I’m hitting the wall with a bug for the email field in my signup form. The form have a number of mandatory fields. If the email field is left blank when I submit the form it feedback the error correctly. But if I enter a valid or invalid email into the field and submit I always get the 404 page. All other fields works fine except for the strange behaviour with the email field.

    Any idea what can be causing this strange behaviour?

    Thanks,

    Hang.

    1. Yes, that is odd. Can you post a link so I can see it in action?

      1. Yes, you can see this in the following link to the signup form

        http://staging.supperclub.creativewiz.org/signup/

        You have to disable javascript on your browser to block my current javascript workaround in order to see the issue. Then enter a valid or invalid email and submit and you will get the 404 page.

        Thanks.

        1. Hi Roland,

          I think I found the issue. I have a custom post type also called Email. Somehow WordPress got confused and processed the email form field as a Email post type. When I changed the email field name to something else it all worked fine.

          Thanks.

        2. Glad to hear your got that figured out. The plugin also has a setting “primary email address field” where you can assign which field is is used for emailing…this is in case you want to change the name of the “email” field.

Leave a Reply
You have to agree to the comment policy.

11 thoughts on “Custom Field Validation

  1. Hi Roland, I have an issue when validating ordinary fields. When an error type field happens, the user is redirected to the posts page. Any idea why this could be happening?
    I read almost all the documentation trying to find any place where to set this but I couldn’t. I only want to show the same signup form with a simple message near the field, without being redirected anywhere.
    Thanks!

    1. That is very unusual behavior. Normally, of course, the user stays on the same page to see the error message and make any changes to the form.

      I can only guess there is some other plugin or your theme that is causing the redirect. If you want to post a link to the form, I’ll take a look at it.

      You may find that using client-side validation works better, you can do this by adding a “required” attribute to the fields…put this in the “values” field of your field definition: required::required

      1. Thanks! that worked !!
        One more question, what code do i put in the fields value of a dropdown menu to select an option by default? And also to make this field required.

        1. The field definition on the Manage Database Fields page has a “default” value you can fill in to set the preselected value. Also, this is where you can make the field required.

  2. Hi,

    Thank you for this great plugin. I’m hitting the wall with a bug for the email field in my signup form. The form have a number of mandatory fields. If the email field is left blank when I submit the form it feedback the error correctly. But if I enter a valid or invalid email into the field and submit I always get the 404 page. All other fields works fine except for the strange behaviour with the email field.

    Any idea what can be causing this strange behaviour?

    Thanks,

    Hang.

    1. Yes, that is odd. Can you post a link so I can see it in action?

      1. Yes, you can see this in the following link to the signup form

        http://staging.supperclub.creativewiz.org/signup/

        You have to disable javascript on your browser to block my current javascript workaround in order to see the issue. Then enter a valid or invalid email and submit and you will get the 404 page.

        Thanks.

        1. Hi Roland,

          I think I found the issue. I have a custom post type also called Email. Somehow WordPress got confused and processed the email form field as a Email post type. When I changed the email field name to something else it all worked fine.

          Thanks.

        2. Glad to hear your got that figured out. The plugin also has a setting “primary email address field” where you can assign which field is is used for emailing…this is in case you want to change the name of the “email” field.

Leave a Reply
You have to agree to the comment policy.