Last active
December 25, 2015 01:50
-
-
Save michaeluno/afd66b40e705ce85cba8 to your computer and use it in GitHub Desktop.
Demonstrates validating multiple fields of the form created by the framework. This is a sample plugin introduced in the tutorial, Validate Submitted Form Data of Multiple Fields (http://en.michaeluno.jp/admin-page-framework/tutorials-v3/08-validate-submitted-form-data-of-multiple-fields/).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?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; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment