Validator Helper
This helper is a collection of functions that allows the user to validate various sources of information against predefined paramaters and is useful most for form submission checks and data entry.
This helper is loaded using the following code:
$this->load->helper('validator');
The following functions are available:
required('var')
This function performs a simple boolean check to ensure a passed variable contains a value.
if(required($var))
{
...code goes here
}
regex('var', 'regex')
This function is a wrapper for php's preg_match function. Passing a mixed var plus the regular expression produces a boolean result.
if(regex($var, 'pattern'))
{
...code goes here
}
has_one_of_these('args')
A simple check to ensure that at least one value within an array exists.
if(has_one_of_these($var))
{
...code goes here
}
valid_email('email')
A simple boolean check to ensure a string conforms to standard email protocol.
$email = 'sample@sampleemail.com';
if(valid_email($email))
{
...code goes here
}
min_num('var', 'limit')
A simple boolean check to ensure a integer is below a certain value.
$var = 123;
$limit = 1000;
if(min_num($var, $limit))
{
...code goes here
}
max_num('var', 'limit')
A simple boolean check to ensure a integer is above a certain value.
$var = 123;
$limit = 1000;
if(max_num($var, $limit))
{
...code goes here
}
is_between('var', 'lo', 'hi')
A simple boolean check to ensure a integer exists between the range of two test integers.
$var = 123;
$lo = 1;
$hi = 200;
if(is_between($var, $lo, $hi))
{
...code goes here
}
is_outside('var', 'lo', 'hi')
A simple boolean check to ensure a integer exists outside the range of two test integers.
$var = 123;
$lo = 1;
$hi = 200;
if(is_outside($var, $lo, $hi))
{
...code goes here
}
length_max('str', 'limit')
A simple boolean check to ensure a string's length is less than the given argument.
$str = 'string content';
$limit = 20;
if(length_max($str, $limit))
{
...code goes here
}
length_min('str', 'limit')
A simple boolean check to ensure a string's length is greater than the given argument.
$str = 'string content';
$limit = 20;
if(length_min($str, $limit))
{
...code goes here
}
valid_phone('str')
A simple boolean check to ensure a string conforms to standard phone number protocol.
$str = '503-234-5678';
if($valid_phone($str))
{
...code goes here
}
is_equal_to('val', 'val2')
A simple boolean check to ensure to mixed vars are equal to each other.
$val1 = 'some value';
$val2 = 'some value';
if(is_equal_to($val1, $val2))
{
...code goes here
}
is_not_equal_to('val', 'val2')
A simple boolean check to ensure to mixed vars are not equal to each other.
$val1 = 'some value';
$val2 = 'some value';
if(is_not_equal_to($val1, $val2))
{
...code goes here
}
is_one_of_these('val', 'search_in')
A boolean check to ensure a value exists within an array.
$val = 'some value';
$array = array($val, $array);
if(is_one_of_these($val, $array))
{
...code goes here
}
is_not_one_of_these('val', 'searchIn')
A boolean check to ensure a value does not exist within an array.
$val = 'some value';
$array = array($val, $array);
if(is_not_one_of_these($val, $array))
{
...code goes here
}
is_of_file_type('fileName', 'fileType')
A boolean check to ensure an uploaded files is equal to the past mime type.
$fileName = 'myfile.txt';
$fileType = 'txt';
if(is_of_file_type($fileName, $fileType))
{
...code goes here
}
valid_file_upload('fileName')
A boolean check to ensure that files exist within a post submission.
$fileName = 'myfile.txt';
if(valid_file_upload($fileName))
{
...code goes here
}
is_safe_character('val', 'allowed')
A simple boolean check to ensure a string consists only of alphanumeric values.
$val = 'string of characters';
$allowed = '+-=';
if(is_safe_character($val, $allowed))
{
...code goes here
}
valid_date_time('date', 'format', 'delimiter')
A boolean check to ensure a date and time string conforms to a past test value.
$date = '2010-03-27';
$format = 'Y-m-d';
$delimiter = '-';
if(valid_date_time($date, $format, $delimiter))
{
...code goes here
}
valid_date('date', 'format', 'delimiter')
A boolean check to ensure a date string conforms to a past test value.
$date = '2010-03-27';
$format = 'Y-m-d';
$delimiter = '-';
if(valid_date($date, $format, $delimiter))
{
...code goes here
}
valid_time('date')
A boolean check to ensure a time string conforms to a past test value.
$date = '2010-03-27 8:00:00';
if(valid_time($date))
{
...code goes here
}
valid_mdy('m', 'd', 'y')
A boolean check to ensure a date and time string conforms to a past test value.
$m = '03';
$d = '27';
$y = '2010';
if(valid_mdy($m, $d, $y))
{
...code goes here
}
is_after_date('date1', 'date2')
A boolean check to ensure a date string exists later than a given value
$date1 = '2010-03-27';
$date2 = '2010-03-27';
if(is_after_date($date1, $date2))
{
...code goes here
}
is_before_date('date1', 'date2')
A boolean check to ensure a date string exists before than a given value
$date1 = '2010-03-27';
$date2 = '2010-03-27';
if(is_before_date($date1, $date2))
{
...code goes here
}
is_between_dates('val', 'date1', 'date2')
A boolean check to ensure a date exists within a given range
$val = '2010-04-01';
$date1 = '2010-03-27';
$date2 = '2010-04-27';
if(is_between_dates($date1, $date2))
{
...code goes here
}
is_future_date('val')
A boolean check to ensure a date exists at some point in the future
$val = '2011-03-27';
if(is_future_date($val))
{
...code goes here
}
is_past_date('val')
A boolean check to esnure a date exists at some point in the past.
display_errors_js('ERRORS')
This function passes user-defined errors to a javascript function and are dsiplayed using javascript alerts.
display_errors('class', 'ERRORS')
This function passes user-defined errors to a javascript function and injects it's content as a child of any element owning the included class.
has_errors()
Returns all user defined global errors assigned to global var GLOBAL_ERRORS.
add_error('msg', 'key')
Add an error message in key/value format to the global errors var GLOBAL_ERRORS.
add_errors('errors')
Add an array of error messages in key/value format to the global errors var GLOBAL_ERRORS.
get_error('key')
Returns an error message from the GLOBAL_ERRORS array if it exists under a particular key.
get_errors()
Returns an array of all error messages from the GLOBAL_ERRORS array if it exists.