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.
Steps
- Include the library
- Extend the library class
- Define the setUp() method
- Define the form
- Define the validation method
- Instantiate the extended class
Screenshot
Include the library
The library file needs to be loaded.
13 |
include( dirname( __FILE__ ) . '/library/apf/admin-page-framework.php' ); |
Extend the library class
The class name needs to be unique. We use APF_ValidateSingleField as an example here.
1 2 |
class APF_ValidateSingleField extends AdminPageFramework { // our code goes 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:
- the root page.
- the sub page title.
- the sub page slug.
The code will look like the following.
15 16 17 18 19 20 21 22 23 24 25 |
class APF_ValidateSingleField extends AdminPageFramework { public function setUp() { $this->setRootMenuPage( __( 'Validate Form Data', 'admin-page-framework-tutorials' ) ); // where to belong $this->addSubMenuItem( array( 'title' => __( '7. Validate Single Field', 'admin-page-framework-tutorials' ), // page and menu title 'page_slug' => 'apf_tutorial_single_field_validation' // page slug ) ); |
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.
27 28 29 30 31 32 33 34 35 36 37 38 |
$this->addSettingFields( array( 'field_id' => 'url', 'title' => __( 'URL', 'admin-page-framework-tutorials' ), 'type' => 'text', 'default' => 'http://wordpress.org', ), array( 'field_id' => 'submit', 'type' => 'submit', ) ); |
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.
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
public function validation_APF_ValidateSingleField_url( $sInput, $sOldInput ) { // Set a flag $_bIsValid = true; // Prepare an field error array. $_aErrors = array(); // Use the debug method to see what are passed. // $this->oDebug->log( $sInput ); // Check if a url is passed if ( ! filter_var( $sInput, FILTER_VALIDATE_URL ) ) { $_bIsValid = false; // $variable[ 'field_id' ] $_aErrors['url'] = __( 'The value must be a url:', 'admin-page-framework-tutorials' ) . ' ' . $sInput; } // An invalid value is found. if ( ! $_bIsValid ) { // Set the error array for the input fields. $this->setFieldErrors( $_aErrors ); $this->setSettingNotice( __( 'There was something wrong with your input.', 'admin-page-framework-tutorials' ) ); return $sOldInput; } return $sInput; } |
Instantiate the extended class
Unless it is instantiated, it will do nothing.
87 |
new APF_ValidateSingleField; |
Code
Try the code below and post comments if you get a problem.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
<?php /* * Plugin Name: Admin Page Framework Tutorial 07 - Validate Single Field * Plugin URI: http://en.michaeluno.jp/admin-page-framework * Description: Validates a single field of the form created by the framework. * Author: Michael Uno * Author URI: http://michaeluno.jp * Version: 1.0.0 * Requirements: PHP 5.2.4 or above, WordPress 3.4 or above. Admin Page Framework 3.0.6 or above */ // Set your own path here include( dirname( __FILE__ ) . '/library/apf/admin-page-framework.php' ); class APF_ValidateSingleField extends AdminPageFramework { public function setUp() { $this->setRootMenuPage( __( 'Validate Form Data', 'admin-page-framework-tutorials' ) ); // where to belong $this->addSubMenuItem( array( 'title' => __( '7. Validate Single Field', 'admin-page-framework-tutorials' ), // page and menu title 'page_slug' => 'apf_tutorial_single_field_validation' // page slug ) ); $this->addSettingFields( array( 'field_id' => 'url', 'title' => __( 'URL', 'admin-page-framework-tutorials' ), 'type' => 'text', 'default' => 'http://wordpress.org', ), array( 'field_id' => 'submit', 'type' => 'submit', ) ); } /** * The pre-defined validation callback method. * * Notice that the method name is validation_{instantiated class name}_{field id}. * * @param string|array $sInput The submitted field value. * @param string|array $sOldInput The old input value of the field. */ public function validation_APF_ValidateSingleField_url( $sInput, $sOldInput ) { // Set a flag $_bIsValid = true; // Prepare an field error array. $_aErrors = array(); // Use the debug method to see what are passed. // $this->oDebug->log( $sInput ); // Check if a url is passed if ( ! filter_var( $sInput, FILTER_VALIDATE_URL ) ) { $_bIsValid = false; // $variable[ 'field_id' ] $_aErrors['url'] = __( 'The value must be a url:', 'admin-page-framework-tutorials' ) . ' ' . $sInput; } // An invalid value is found. if ( ! $_bIsValid ) { // Set the error array for the input fields. $this->setFieldErrors( $_aErrors ); $this->setSettingNotice( __( 'There was something wrong with your input.', 'admin-page-framework-tutorials' ) ); return $sOldInput; } return $sInput; } } new APF_ValidateSingleField; |