-
Notifications
You must be signed in to change notification settings - Fork 194
Helper Validation
Jeff Johns edited this page Jan 31, 2014
·
7 revisions
Name | Path |
---|---|
Validation Helper | /application/helpers/validation_helper.php |
These methods will be your sanity checking methods. You can easily pass an array of data in and figure out if they are valid or not. Errors will be returned for each item that does not pass the evaluation.
$this->load->helper('validation_helper');
Return back if the value passed in is valid against the type.
Name | Description |
---|---|
bool | Currently bool just checks if the variable is empty, if empty then return false, else return true |
date | Checks for a date in the following format: 0000-00-00 |
datetime | Checks for a date/time in the following format: 0000-00-00 00:00:00 |
domain | Checks for a domain in the following format: /[A-Z0-9._-]+\.[A-Z]{2,}/i |
regex to validate an email address | |
md5 | Checks for a string with a length of 32. |
numeric | checks if the variable is a positive number |
password | regex to validate a password (min 6 characters, at least one uppercase letter and one number) |
slug | Checks for a valid slug. Letter, numbers and dashes only. |
string | Checks to make sure the value is a string. |
time | Checks for a time in the following format: 00:00:00 |
url | Checks to see if a scheme and host exist. |
variable | Checks for to make sure it is a valid PHP variable. |
Variable | Type | Default | Required | Description |
---|---|---|---|---|
$val | mixed | N/A | Yes | The value to evaluate |
$type | string | N/A | Yes | The type to validate against |
if (is_valid($password, 'password')) {
// Have fun
}
Allows you to pass three arrays to the method. Each one containing important information on what to evaluate, how to evaluate it and if it is required or not.
It is easiest to set your $data_types
argument in the constructor of the model for the model you are building to keep it DRY.
This method is nice to evaluate a large portion of options at once. It calls is_valid
from within the method to evaluate each item in the $options
array submitted.
Variable | Type | Default | Required | Description |
---|---|---|---|---|
$options | array | array() | Yes | An array of options to evaluate. The key should be the database column name and value the value being evaluated |
$data_types | array | array() | Yes | An array of data types to be used to evaluate the options. The key is the column name and the value applies to one of the data types in the `is_valid` method. |
$required | array | array() | No | An array of column names that are required |
$required = array('email','password');
$options = array('email'=>'[email protected]','password'=>'Password123');
$data_types = array('email' => 'email', 'password' => 'password');
if (validate($options, $data_types, $required)) {
//Yeah, you did it
}