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.
16 thoughts on “Custom Field Validation”
These filters work great thanks. What could we do if we need to validate a field depending on the input in another field?
The $field object has the record id available in it as $field->record_id, so you can use that to get the full data from the stored record. If you need to validate againse the submitted data instead, you can get that by accessing the $_POST array.
Thanks it works perfectly with the $_POST array !
One last question, is there a way to know that the user selected the value Other in the dropdown or it’s overwritten by the input?
I have a lot of values in the dropdown so that’d be nice not to compare with all of them
Hello. I’m wondering if you can help me. I believe if I build off from this thread I can accomplish what I am looking for but I’m not quite sure where to place the code.
I’d like the signup to be smart enough to give me a validation alert if a phone number has been entered in the last 30 days.
I’m not sure how to accomplish this but this thread appears to be the closest template to work off from. Does anyone have any ideas on how I can accomplish what I am looking to do?
I’m trying to understand what you’re looking for here. Signup forms create new records, so there won’t be any way to measure the 30 days you mention.
Anyway, it sounds like you maybe need to install and configure the plugin and get to know how it works before you get into confugring a special setup like you are asking about. This won’t have nything to do with validation, that is just used to make sure that data is entered correctly.
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.
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
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.
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.
I just figured it out, thanks!
Please check this page is not working
Thanks, looks like a broken link…I’ve fixed it
the correct URL is: https://xnau.com/work/wordpress-plugins/participants-database/participants-database-documentation/defining-participants-database-fields/useful-selector-values/
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?
Yes, that is odd. Can you post a link so I can see it in action?
Yes, you can see this in the following link to the signup form
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.
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.