Validating a Set of Fields
Now we know a way of validating a single field with the pre-defined callback method. What if you want to validate multiple fields at once? There are other pre-defined validation callback methods.
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.
1 2 3 |
class APF_ValidateFields extends AdminPageFramework { // our code comes 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.
This time, we are going to add a sub page to the root page created in the plugin introduced in the previous tutorial. To specify a root menu item created by the framework in a separate script, use the setRootMenuPageBySlug() method and pass the class name that defines the root menu item.
The code will look like the following.
17 18 19 20 21 22 23 24 25 |
public function setUp() { $this->setRootMenuPageBySlug( 'APF_ValidateSingleField' ); // where to belong $this->addSubMenuItem( array( 'title' => __( '8. Validate Fields', 'admin-page-framework-tutorials' ), // page and menu title 'page_slug' => 'apf_tutorial_fields_validation' // page slug ) ); |
Define the Form
We are going to create a section and two fields and one submit button.
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
$this->addSettingSections( array( 'section_id' => 'text_section', 'page_slug' => 'apf_tutorial_fields_validation', ) ); $this->addSettingFields( 'text_section', // the target section ID array( 'field_id' => 'alpanumeric', 'title' => __( 'Alpanumeric', 'admin-page-framework-tutorials' ), 'type' => 'text', ), array( 'field_id' => 'no_html', 'title' => __( 'No HTML', 'admin-page-framework-tutorials' ), 'type' => 'textarea', ), array( 'field_id' => 'submit', 'type' => 'submit', ) ); |
Define the Validation Method
We make the first field only accept an alphanumeric value and the second field have no HTML tags. Notice the method name is validation_ + instantiated class name + _ + section ID.
The first parameter receives an array holding the submitted field values of the section. The second parameter receives the stored field array of the section. Note that in the previous tutorial, the variable types were strings because only the specified individual field value was passed.
Also, to construct a field error array, this time, set the section ID in the first dimension and set the field ID in the second dimension.
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
public function validation_APF_ValidateFields_text_section( $aInputs, $aOldInputs, $oFactory, $aSubmitInfo ) { // Prepare a flag and a field error array. $_fIsValid = true; $_aErrors = array(); // Check if the value is alphabetical & numeric if ( isset( $aInputs[ 'alpanumeric' ] ) && ! ctype_alnum( $aInputs[ 'alpanumeric' ] ) ) { $_fIsValid = false; // $variable[ 'sectioni_id' ][ 'field_id' ] $_aErrors[ 'text_section' ][ 'alpanumeric' ] = __( 'The value must be alphabetic and numeric:', 'admin-page-framework-tutorials' ) . ' ' . $aInputs[ 'alpanumeric' ]; } // Sanitize the value - strip HTML tags $aInputs[ 'no_html' ] = isset( $aInputs[ 'no_html' ] ) ? strip_tags( $aInputs[ 'no_html' ] ) : ''; // An invalid value is found. if ( ! $_fIsValid ) { // 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 $aOldInputs; } |
Instantiate the extended class
Finally, we instantiate the class. Unless it is instantiated, it will do nothing.
106 |
new APF_ValidateFields; |
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 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
<?php /* * Plugin Name: Admin Page Framework Tutorial 08 - Validate Submitted Form Data * Plugin URI: http://en.michaeluno.jp/admin-page-framework * Description: Validate the form data of a section. * Author: Michael Uno * Author URI: http://michaeluno.jp * Version: 1.0.1 * 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_ValidateFields extends AdminPageFramework { public function setUp() { $this->setRootMenuPageBySlug( 'APF_ValidateSingleField' ); // where to belong $this->addSubMenuItem( array( 'title' => __( '8. Validate Fields', 'admin-page-framework-tutorials' ), // page and menu title 'page_slug' => 'apf_tutorial_fields_validation' // page slug ) ); $this->addSettingSections( array( 'section_id' => 'text_section', 'page_slug' => 'apf_tutorial_fields_validation', ) ); $this->addSettingFields( 'text_section', // the target section ID array( 'field_id' => 'alpanumeric', 'title' => __( 'Alpanumeric', 'admin-page-framework-tutorials' ), 'type' => 'text', ), array( 'field_id' => 'no_html', 'title' => __( 'No HTML', 'admin-page-framework-tutorials' ), 'type' => 'textarea', ), array( 'field_id' => 'submit', 'type' => 'submit', ) ); } /** * The pre-defined validation callback method. * * Notice that the name of the method is made up of validation_{instantiated class name}_{section id}. * * The following hooks are available: * - validation_{instantiated class name}_{field id} – [3.0.0+] receives the form submission value of the field that does not have a section. The first parameter: ( string|array ) submitted input value. The second parameter: ( string|array ) the old value stored in the database. * - validation_{instantiated class name}_{section_id}_{field id} – [3.0.0+] receives the form submission value of the field that has a section. The first parameter: ( string|array ) submitted input value. The second parameter: ( string|array ) the old value stored in the database. * - validation_{instantiated class name}_{section id} – [3.0.0+] receives the form submission values that belongs to the section.. The first parameter: ( array ) the array of submitted input values that belong to the section. The second parameter: ( array ) the array of the old values stored in the database. * - validation_{page slug}_{tab slug} – receives the form submission values as array. The first parameter: submitted input array. The second parameter: the original array stored in the database. * - validation_{page slug} – receives the form submission values as array. The first parameter: submitted input array. The second parameter: the original array stored in the database. * - validation_{instantiated class name} – receives the form submission values as array. The first parameter: submitted input array. The second parameter: the original array stored in the database. * * @param array $aInputs The submitted field array. * @param array $aOldInputs The stored field array. * @callback filter validation_{instantiated class name}_{section id} */ public function validation_APF_ValidateFields_text_section( $aInputs, $aOldInputs, $oFactory, $aSubmitInfo ) { // Prepare a flag and a field error array. $_fIsValid = true; $_aErrors = array(); // Check if the value is alphabetical & numeric if ( isset( $aInputs[ 'alpanumeric' ] ) && ! ctype_alnum( $aInputs[ 'alpanumeric' ] ) ) { $_fIsValid = false; // $variable[ 'sectioni_id' ][ 'field_id' ] $_aErrors[ 'text_section' ][ 'alpanumeric' ] = __( 'The value must be alphabetic and numeric:', 'admin-page-framework-tutorials' ) . ' ' . $aInputs[ 'alpanumeric' ]; } // Sanitize the value - strip HTML tags $aInputs[ 'no_html' ] = isset( $aInputs[ 'no_html' ] ) ? strip_tags( $aInputs[ 'no_html' ] ) : ''; // An invalid value is found. if ( ! $_fIsValid ) { // 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 $aOldInputs; } // The return values will be saved in the database. return $aInputs; } } new APF_ValidateFields; |
I have repeatable, sortable, tabs. When validation on one of them fails and I call setFieldErrors, the error is displayed in red on all 3 tabs.
There is no way for me to target the tab with an error.
Like
$_aErrors[ ‘tab_section’ ][3][ ‘alpanumeric’ ] =’Some error text’;
Instead of
$_aErrors[ ‘tab_section’ ][ ‘alpanumeric’ ] =’Some error text’;