Skip to content
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.

Load the helper

$this->load->helper('validation_helper');

Methods

isValid

Return back if the value passed in is valid against the type.

Types

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
email 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.

Arguments

Variable Type Default Required Description
$val mixed N/A Yes The value to evaluate
$type string N/A Yes The type to validate against

Example

if (is_valid($password, 'password')) {
    // Have fun
}

validate

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.

Arguments

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

Example

$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
}