Skip to content

Instantly share code, notes, and snippets.

@tommcfarlin
Last active August 29, 2015 14:22
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 tommcfarlin/edc6a7cd0226c78b7dd2 to your computer and use it in GitHub Desktop.
Save tommcfarlin/edc6a7cd0226c78b7dd2 to your computer and use it in GitHub Desktop.
[WordPress] Validating Input via the WordPress Settings API
<?php
interface Input_Validator {
public function __construct( $setting );
public function is_valid( $input );
}
<?php
/**
* This class is responsible for validating the address field
* as provided by the user in the Dashboard.
*
* @since 1.0.0
*
* @implements Input_Validator
* @package Acme/classes
*/
class Address_Validator implements Input_Validator {
/**
* Slug title of the setting to which this error applies
* as defined via the implementation of the Settings API.
*
* @access private
*/
private $setting;
/**
* Creates an instance of the class and associates the
* specified setting with the property of this class.
*
* @param string $setting The title of the setting we're validating
*/
public function __construct( $setting ) {
$this->setting = $setting;
}
/**
* Determines if the specified input is valid. For purposes of addresses,
* we only want to make sure the user isn't specifying an empty string.
*
* @param string $input The address string
* @return bool True if the input is valid; otherwise, false
*/
public function is_valid( $input ) {
$is_valid = true;
// If the input is an empty string, add the error message and mark the validity as false
if ( '' == trim( $input ) ) {
$this->add_error( 'invalid-address', 'You must provide a valid address.' );
$is_valid = false;
}
return $is_valid;
}
/**
* Adds an error message to WordPress' error collection to be displayed in the dashboard.
*
* @access private
*
* @param string $key The key to which the specified message will be associated
* @param string $message The message to display in the dashboard
*/
private function add_error( $key, $message ) {
add_settings_error(
$this->setting,
$key,
$message,
'error'
);
}
}
<?php
/**
* This class is responsible for validating the city field
* as provided by the user in the Dashboard.
*
* @since 1.0.0
*
* @implements Input_Validator
* @package Acme/classes
*/
class City_Validator implements Input_Validator {
/**
* Slug title of the setting to which this error applies
* as defined via the implementation of the Settings API.
*
* @access private
*/
private $setting;
/**
* Creates an instance of the class and associates the
* specified setting with the property of this class.
*
* @param string $setting The title of the setting we're validating
*/
public function __construct( $setting ) {
$this->setting = $setting;
}
/**
* Determines if the specified input is valid. For purposes of cities,
* we only want to make sure the user isn't specifying an empty string.
* This can be more complicated, though, if you wanted to compare it against
* an array of cities, etc.
*
* @param string $input The city string
* @return bool True if the input is valid; otherwise, false
*/
public function is_valid( $input ) {
$is_valid = true;
// If the input is an empty string, add the error message and mark the validity as false
if ( '' == trim( $input ) ) {
$this->add_error( 'invalid-city', 'You must provide a valid city.' );
$is_valid = false;
}
return $is_valid;
}
/**
* Adds an error message to WordPress' error collection to be displayed in the dashboard.
*
* @access private
*
* @param string $key The key to which the specified message will be associated
* @param string $message The message to display in the dashboard
*/
private function add_error( $key, $message ) {
add_settings_error(
$this->setting,
$key,
$message,
'error'
);
}
}
<?php
/**
* This class is responsible for validating a postal code
* as provided by the user in the Dashboard.
*
* @since 1.0.0
*
* @implements Input_Validator
* @package Acme/classes
*/
class Postal_Code_Validator implements Input_Validator {
/**
* Slug title of the setting to which this error applies
* as defined via the implementation of the Settings API.
*
* @access private
*/
private $setting;
/**
* Creates an instance of the class and associates the
* specified setting with the property of this class.
*
* @param string $setting The title of the setting we're validating
*/
public function __construct( $setting ) {
$this->setting = $setting;
}
/**
* Determines if the specified input is valid. For these postal codes,
* we're verifying the input that the user provides against a regular
* expression that verifies it's a valid Canadian postal code
*
* @param string $input The postal code string
* @return bool True if the input is valid; otherwise, false
*/
public function is_valid( $input ) {
$is_valid = true;
// Use the following RegEx to determine if the specified zip code is of proper Canadian format
$is_valid =
preg_match (
'$[ABCEGHJKLMNPRSTVXY][0-9][ABCEGHJKLMNPRSTVWXYZ] ?[0-9][ABCEGHJKLMNPRSTVWXYZ][0-9]$',
$input
);
// If the input is an empty string, add the error message and mark the validity as false
if ( ! $is_valid ) {
$this->add_error( 'invalid-postal-code', 'You must provide a valid postal code.' );
$is_valid = false;
}
return $is_valid;
}
/**
* Adds an error message to WordPress' error collection to be displayed in the dashboard.
*
* @access private
*
* @param string $key The key to which the specified message will be associated
* @param string $message The message to display in the dashboard
*/
private function add_error( $key, $message ) {
add_settings_error(
$this->setting,
$key,
$message,
'error'
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment