Form Validation in Bonfire


Codeigniter provides the ‘form validation’ library, which is used by the bonfire.  In bonfire the library is already loaded for you in every controller, so it is optional to load the library.
Here is the code in the controller that you use to form validate

public function create()
    {
        // models loading part
        $this->load->model('users/User_model', 'user_model');
       
        // submission part
        if ($this->input->post('submit'))
        {
            $this->form_validation->set_rules('user_type', 'User Type', 'required');
            $this->form_validation->set_rules('account_name_text', 'Account Name', 'callback_account_name');
            $this->form_validation->set_rules('email', 'Email', 'required|trim|strip_tags|valid_email|callback_unique_email|xsx_clean');
            $this->form_validation->set_rules('password', 'Password', 'required');
            if ($this->form_validation->run() !== false)
            {
                $account_name = (($this->input->post('user_type')=='mainuser')? $this->input->post('account_name_text') : $this->input->post('account_name_dropdown'));
                $data = array(
                    'user_type'   => $this->input->post('user_type'),
                    'account_name'   => $account_name,
                    'first_name'   => $this->input->post('first_name'),
                    'last_name'   => $this->input->post('last_name'),
                    'email'   => $this->input->post('email'),
                    'password'   => $this->input->post('password'),
                    'street_1'   => $this->input->post('street_1'),
                    'street_2'   => $this->input->post('street_2'),
                    'city'   => $this->input->post('city'),
                    'zipcode'   => $this->input->post('zipcode'),
                );

                $user_id = $this->user_model->add_user($data);
                if($user_id)
                {
                    Template::set_message('User added','success');
                    redirect('/admin/custom/users');
                }
                else
                {
                    Template::set_message('User could not be added','attention');
                }
            }
        }
        Template::set('data',$output);
        Template::set_view('admin/custom/create_user');
        Template::render();
    }

Here 'callback_account_name' is the custom callback function to run when account_name_text field is being checked. Here is an example:

public function account_name($account_name_text)
    {
        if(($this->input->post('user_type')=='mainuser') && (strtolower($account_name_text)=='uncategorised'))
        {
            $this->form_validation->set_message('account_name','Cannot create Uncategorised account.');
            return false;
        }
       
    }

You have to define this within the same controller class. The set_message() function gives the message for error return on validation. Note that the function name should not include the ‘callback_’ prefix and set_message() should have first parameter equal to the function name.
Here is another email validation callback function code.

public function unique_email($email)
    {
        if ($this->user_model->is_unique('email', $email) === true)
        {
            return true;
        }
        else
        {
            $this->form_validation->set_message('unique_email', 'Email is already in use.');
            return false;
        }
    }