\io_comand_web\validate
Validate query parameters and submitted form fields.
Example
The following example validates field values for a form submission.
// Validate field values from a form submission
$validate = new \io_comand_web\validate();
$email = $validate->request('email',
['trim','required','email'],
'Invalid email address'
);
if($validate->valid()){
echo('Email is valid');
}
Preparations & Validations
Any number of preparation and validation methods can be specified to prepare and validate a value. Each is specified in an array and processed in sequence. The first validation to fail will trigger a validation error. If all validations are successful then the value will pass validation.
There are three ways to specify a preparation and validation methods:
- String with name - the simplest form is to reference the preparation or validation method with a string like 'trim' or 'required'.
- Array with class and name - Specify the class and preparation or validation method in a two-element array like [\io_comand_web\validate,'trim']. The class can also be specified as an object of the class like [$validate,'trim'].
- Array with class (optional), name and parameters - If a preparation or validation method takes parameters, you can provide them after the name in an array like ['min_length', 10] or [\io_comand_web\validate,'min_length',10].
Preparations and validations will be searched in the following order and the first match will be used.
- If a class/object is specified, check the corresponding class for a validation method match.
- If a class/object is specified, check the corresponding class for a preparation method match.
- Check the calling class for a custom validation match.
- Check the calling class for a custom preparation match.
- Check for a built-in validation custom method match.
- Check for a built-in preparation custom method match.
Built-in Preparation Methods
- trim - trim leading and trailing whitespace, including spaces, tabs ('\t'), newlines ('\n') and carriage returns ('\r').
Built-in Validation Methods
- alpha - Ensure a value contains only alphabetic characters.
- card_cv - Ensure a value is a valid credit card CVV number (3-4 digits).
- card_expire - Ensure a value is a valid credit card expiration date (MM/YYYY).
- card_number - Ensure a value is a valid credit card number (contains 15-16 digits).
- email - Ensure a value is a valid email address.
- integer - Ensure a value is a valid integer.
- min_length(length) - Ensure a value is at least a certain number of characters.
- phone - Ensure a value is a valid phone number.
- required - Ensure a value is specified. More specifically, not NULL, FALSE or an empty string.
- true - Ensure a value evaluates to TRUE. In PHP terms, ($value == TRUE), not ($value === TRUE).
- uuid - Ensure a value is a valid UUID in dash-separated format.
Custom Preparation Methods
A custom preparation method can be defined in any class and referenced in the list of preparations and validations. A preparation method name must start with "prepare__".
Example
class my_class {
function web__submit() {
// Validate field values with a custom preparation
$validate = new \io_comand_web\validate();
$phone = $validate->request('phone',
['just_numbers','required',['min_length',10]],
'Invalid phone number (must contain at least 10 digits).'
);
if($validate->valid()){
echo('Phone number is valid');
}
}
// remove all characters except numbers
function prepare__just_numbers($value) {
return preg_replace('/[^0-9]/', '', $value);
}
}
Custom Validation Methods
Similar to custom preparation methods, custom validation methods can be defined in a class by prefixing a method with "validate__".
Example
class my_class {
function web__submit() {
// Validate field values with a custom preparation
$validate = new \io_comand_web\validate();
$phone = $validate->request('phone',
['trim','required','phone'],
'Invalid phone number.'
);
if($validate->valid()){
echo('Phone number is valid');
}
}
// verify value is a 10-digit phone number
function validate__phone($value) {
if($value != '') {
if(!preg_match('/^\(?[0-9]{3}\)?[-. ]?[0-9]{3}[-. ]?[0-9]{4}/', $value)) {
return FALSE;
}
}
return TRUE;
}
}