Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save michaeluno/11385830 to your computer and use it in GitHub Desktop.
Save michaeluno/11385830 to your computer and use it in GitHub Desktop.
Demonstrates the use of pre-defined validation callback methods in Admin Page Framework.
<?php
/* Plugin Name: Admin Page Framework Demo - Field Validation */
if ( ! class_exists( 'AdminPageFramework' ) ) {
include_once( dirname( dirname( __FILE__ ) ) . '/admin-page-framework/development/admin-page-framework.php' );
}
class APFDemo_FieldValidation extends AdminPageFramework {
public function setUp() {
$this->setRootMenuPage( __( 'Field Validation', 'admin-page-framework-demo-field-validation' ) ); // where to belong
$this->addSubMenuItem(
array(
'title' => __( 'Field Validation', 'admin-page-framework-demo-field-validation' ), // page and menu title
'page_slug' => 'apf_demo_field_validation' // page slug
)
);
$this->addSettingSections(
array(
'section_id' => 'text_section',
'page_slug' => 'apf_demo_field_validation',
)
);
$this->addSettingFields(
'text_section', // the target section ID
array(
'field_id' => 'url',
'title' => __( 'URL', 'admin-page-framework-demo-field-validation' ),
'type' => 'text',
'default' => 'http://wordpress.org',
),
array(
'field_id' => 'alpanumeric',
'title' => __( 'Alpanumeric', 'admin-page-framework-demo-field-validation' ),
'type' => 'text',
),
array(
'field_id' => 'no_html',
'title' => __( 'No HTML', 'admin-page-framework-demo-field-validation' ),
'type' => 'textarea',
),
array(
'field_id' => 'submit',
'type' => 'submit',
)
);
}
/**
* The pre-defined validation callback method.
*
* 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.
*/
public function validation_APFDemo_FieldValidation_text_section( $aInput, $aOldInput ) { // validation_{instantiated class name}_{section id}
// Set a flag
$_fIsValid = true;
// Prepare an field error array.
$_aErrors = array();
// Use the debug method to see what are passed.
// $this->oDebug->logArray( $aInput );
// Check if a url is passed
if ( isset( $aInput['url'] ) && ! filter_var( $aInput['url'], FILTER_VALIDATE_URL ) ) {
$_fIsValid = false;
// $variable[ 'sectioni_id' ][ 'field_id' ]
$_aErrors['text_section']['url'] = __( 'The value must be a url:', 'admin-page-framework-demo-field-validation' ) . ' ' . $aInput['url'];
}
// Check if the value is alphabetical & numeric
if ( isset( $aInput['alpanumeric'] )&& ! ctype_alnum( $aInput['alpanumeric'] ) ) {
$_fIsValid = false;
// $variable[ 'sectioni_id' ][ 'field_id' ]
$_aErrors['text_section']['alpanumeric'] = __( 'The value must be a alphabetic and numeric:', 'admin-page-framework-demo-field-validation' ) . ' ' . $aInput['url'];
}
// Sanitize the value - strip HTML tags
$aInput['no_html'] = isset( $aInput['no_html'] ) ? $aInput['no_html'] : '';
$aInput['no_html'] = strip_tags( $aInput['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-demo-field-validation' ) );
return $aOldInput;
}
return $aInput;
}
}
new APFDemo_FieldValidation;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment