CodeIgniter library for fields form validation. It is independent of the standard library of CodeIgniter and can also be used for Ajax calls.
###How to install
- Add the file Validation.php to your /application/libraries folder.
###How to use
-
Load library:
$this->load->library('validation');
-
Load data from:
a. POST (default):
$this->validation->set_post();
b. GET:
$this->validation->set_get();
c. Array data:
$this->validation->set_data($data);
-
Load your rules:
$this->validation->required…
####Public functions List
set_data
Set fields data from arrayset_post
Set fields data from POSTset_get
Set fields data from GETget_error
Return error data: message and field (if exists)get_error_message
Return error messageget_error_field
Return error fieldis_valid
Check if form is validset_not_valid
Set form not valid ad assign message errorrequired
Check required fieldsrequired_isset
Check required fields only if is setemail
Check if email fields are validregexp
Check that the fields meet a particular regular expressionurl
Check URL fields are validmaxlen
Check that fields are not longer than a defined valueminlen
Check that fields are not shorter than a defined valuealpha
Check that fields do not have characters other than letters of alphabetalpha_s
Check that fields do not have characters other than letters of alphabet and spacenum
Check that fields do not have characters other than numbersnum_s
Check that fields do not have characters other than numbers and spacealphanum
Check that fields do not have characters other than letters and numbersalphanum_s
Check that fields do not have characters other than letters of alphabet, numbers and spaceno_spaces
Check that fields do not have spacesnum_gt
Check if a numeric field is greater than a certain valuenum_lt
Check if a numeric field is less than a certain valuedate
Check that the fields have a datedate_gt
Check that a date is larger than otherdate_lt
Check that a date is smaller than otherdate_en
Check that the fields have a English datedatetime
Check that the fields have a datetime valuechecked
Check that the field has been checkedselected
Check that the field has been selectedequal
Check that the two fields are equalcallback
Check the correctness of the field $param through the method $method.
###How to work
public function save() {
$this->load->library('validation');
$this->validation->set_data($data);
// $this->validation->set_post();
$this->validation->required(array('email', 'username', 'firstname', 'lastname', 'city', 'password'), 'Fields are required'),
->email('email', 'Email is not valid field')
->maxlen('username', 32, 'Username cannot be longer than 32 characters')
->minlen('username', 6, 'Username cannot be shorter than 6 characters')
->regxp('username', '/^([a-zA-Z0-9\-]*)$/i', 'Username cannot have characters other than letters, numbers and hyphens')
->callback('_unique', 'Username already exists.', 'username')
->callback(array($your_model, 'method_name'), 'Error message', array('parameter1', 'parameter2', 'parameter3'))
->callback('_other_func', 'Your error.');
if ($this->validation->is_valid()) {
if ($data['username'] == 'admin')
$this->validation->set_not_valid('Username is already registered');
}
if ($this->validation->is_valid())
echo 'success!';
else
echo $this->validation->get_error_message();
}
public function _unique($username) {
$res = $this->db->query('…');
return $res->id != 0;
}
public function _other_func() {
// Check something
return $error;
}