BackendPro User Guide Version 0.3.1


Validation Class

The Validation Class extends the default CodeIgniter Validation Class by adding some extra callback functions needed by the BackendPro system. It also provides a way to transfer error messages to use the Status Class message system.

How to use the Validation Class?

$this->load->library('validation');

The class file can be found at system/application/libraries/MY_Validation.php

Validation Class Function Reference

$this->validation->output_errors()

Calling this method will transfer any form error messages into the Status Class message system.

$this->validation->set_default_value()

Set Default Form Field:

$this->validation->set_default_value(field,value);

When used as above it will set the initial value of the specified field. Another method of using this method is to give it an associtive array of field names to values.

$data = array('field1' => 'value1', 'field2' => 'value2');
$this->validation->set_default_value($data);

The main reason for using this method over other ways of setting the default value of a form field is this will allow checkboxes and radio buttons to have a default value. The downside of this is on setting the form up for the first time, you must not specify any field rules. If you do the form will submit. So an example of how this would be handled is below.

function myform()
{
    $fields = array(.....);
    $rules = array(.....);
    $this->validation->set_fields($fields);
    
    if( ! $this->input->post('submit'))
    {
        // This is the initial form load
        // Setup form default values
        $data = array(....);
        $this->validation->set_default_values($data);
    }
    else
    {
        // Form submited
        // Validate form input
        $this->validation->set_rules($rules);
    }
    
    if($this->validation->run() === FALSE)
    {
        // Validation FAILED
        // Display form
    }
    else
    {
        // Validation PASSED
        // Save form contents
    }
}

Callback Functions

These are the extra callback functions which can be used in validation rules.

Rule Parameter Description
valid_captcha No Returns FALSE if the form captcha is invalid
spare_username No Returns FALSE if the provided string is already in use as a username for another user
spare_email No Returns FALSE if the provided email is already in use by another user
spare_edit_username No Returns FALSE if the provided string is already in use as a username by another user apart from by the user whos details are being changed. This should be used instead of spare_username when editing a users details.
spare_edit_email No Returns FALSE if the provided string is already in use as an email by another user apart from by the user whos details are being changed. This should be used instead of spare_email when editing a users details.