Validating Field of Submitted Form

Validating a Field

So far, we have covered creating form fields. Now, what if the user submits invalid values which should not be saved in the database? You would need to validate the form data the user submits prior to saving them.

The framework has pre-defined callback methods that allows you to check the submitted values so that you can sanitize or discard them when necessary along with a warning message.


  1. Include the library
  2. Extend the library class
  3. Define the setUp() method
  4. Define the form
  5. Define the validation method
  6. Instantiate the extended class


Validating Field of Submitted Form with Admin Page Framework

Include the library

The library file needs to be loaded.

Extend the library class

The class name needs to be unique. We use APF_ValidateSingleField as an example here.

Define the setUp() method

In the setUp() method, we define the admin pages that are going to be created. For that, we need to decide:

  1. the root page.
  2. the sub page title.
  3. the sub page slug.

The code will look like the following.

Define the Form

Let’s keep it minimal for the demonstration purpose so that you’ll easily see how it works. We are going to add only one field and a submit button.

Define the Validation Method

Say, the user is supposed to type a url in the text field. So we need to check if the typed sting is a url in the validation callback method. If the user submits something not a url, we set an error notification message.

Notice that the method name is validation_ + instantiated class name + field id. There are two parameters that the method accepts. The former is for the user’s input data and the latter is the stored data in the database for this particular field. So you check the passed first parameter value and if it is invalid, return the second parameter value so no change will be made. In this example, the filter_var() function is used to check if the passed value is a url. If you want it to be numeric, you may use is_numeric() in your script, depending on what you need.

There are two ways to display error messages to the user. One is to use the admin setting notice with the setSettingNotice() method. It will internally store the passed message into the temporary area of the database and displays it in the next page load at the top of the page.

The other type of messages is dealt with the setFieldErrors() method. To use that, you need to create a field error argument array. The array structure is that the keys are field IDs when no section is set. We’ll go over the case that a section is set in the next tutorial. Similar to the settings admin notice, the field error array is stored in the temporary area of the database and each field error message is displayed just above the field output in red.

Instantiate the extended class

Unless it is instantiated, it will do nothing.


Try the code below and post comments if you get a problem.

Leave a Reply

Your email address will not be published. Required fields are marked *