ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

Validation callbacks into Models

January 24, 2008 5:48pm

Subscribe [6]
  • #16 / Aug 27, 2008 7:29pm

    beemr

    160 posts

    CI 1.7 is out now, and the validation lib has been revamped for consistency and clarity.  I modified this extension so that I might yet bask in its radiance.  So far so good, but since I don’t use Modular Extensions, I can’t verify whether that connection is still intact.  I’ve kept the $parent variable alive, though it required a slightly awkward tweak.  Modular users, please take a look at my note on the wiki, and let us know how it goes.

    Gracias,

  • #17 / Feb 15, 2010 11:46am

    lordmontie

    7 posts

    Has anyone ever thought of doing something like this?

    Create a new MY_Form_validation.php library file in your application/libraries folder:

    <?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
    
    class MY_Form_validation extends CI_Form_validation {
        
        protected $account            = NULL;
        
        // Checks to see if username is avaliable
        protected function username($username)
        {
            $this->CI->load->model('accounts_model');
            
            $account = $this->CI->accounts_model->get_record(array('username' => $username));
            
            if (FALSE !== $account)
            {
                $this->set_message('_check_username', $username . $this->CI->lang->line('form_val_check_username'));
                return FALSE;
            }
            
            return TRUE;
        }
        
        // Checks to see if username exists in DB
        protected function username_exists($username)
        {
            $this->CI->load->model('accounts_model');
            
            $account = $this->CI->accounts_model->get_record(array('username' => $username));
            
            if (FALSE === $account || !is_array($account) || empty($account) || Accounts_Model::STATUS_ACTIVE != $account['status'])
            {
                $this->set_message('_check_username_exists', $username . $this->CI->lang->line('form_val_check_username_exists'));
                return FALSE;
            }
            
            $this->account = $account;
            return TRUE;
        }
        
        // Checks password with account auth_key
        protected function login($password)
        {
            $this->CI->load->library('Authentication');
            
            if (FALSE === $this->CI->authentication->login($this->account['username'], $this->account['auth_key'], $password))
            {
                $this->set_message('_check_master_auth_key', $this->CI->lang->line('form_val_check_master_auth_key'));
                return FALSE;
            }
            
            return TRUE;
        }
        
        // Checks invite code
        protected function invite($invite_id)
        {
            $this->CI->load->model('invites_model');
            
            $invite = $this->CI->invites_model->get_record(array('invite_id' => $invite_id));
            
            if (FALSE !== $invite)
            {
                $this->set_message('_check_invite', $invite_id . $this->CI->lang->line('form_val_check_invite'));
                return FALSE;
            }
            
            return TRUE;
        }
        
        // Checks invite code
        protected function invite_valid($invite_id)
        {
            $this->CI->load->model('invites_model');
            $this->CI->load->model('invite_accounts_model');
            
            $invite = $this->CI->invites_model->get_record(array('invite_id' => $invite_id));
            
            if (FALSE === $invite || !is_array($invite) || empty($invite))
            {
                $this->set_message('_check_invite_valid', $this->CI->lang->line('form_val_check_invite_valid'));
                return FALSE;
            }
            
            if ($this->CI->invite_accounts_model->count(array('invite_id' => $invite_id) >= $invite['allowed'])
            {
                $this->set_message('_check_invite_valid', $this->CI->lang->line('form_val_check_invite_valid'));
                return FALSE;
            }
            
            return TRUE;
        }
    }
    
    /* End of file MY_Form_validation.php */
    /* Location: ./app/libraries/MY_Form_validation.php */


    Then in your controller use the validation functions like so:

    $this->form_validation->set_rules('username', $this->lang->line('field_username'), 'trim|required|valid_email|max_length[250]|username_exists');
            $this->form_validation->set_rules('password', $this->lang->line('field_password'), 'required|min_length[5]|login');
            
            if (FALSE === $this->form_validation->run())
            {
                $this->data['message'] = $this->session->flashdata('message');
                $this->data['error'] = $this->session->flashdata('error');
                $this->session->keep_flashdata('redirect');
                $this->load->view('account/login', $this->data);
                return;
            }

    OR

    $this->form_validation->set_rules('invite', $this->lang->line('field_invite'), 'trim|required|alpha_numeric|max_length[50]|strtolower|invite_valid');
            $this->form_validation->set_rules('username', $this->lang->line('field_username'), 'trim|required|valid_email|max_length[250]|username');
            $this->form_validation->set_rules('password', $this->lang->line('field_password'), 'required|min_length[5]|matches[passconf]');
            $this->form_validation->set_rules('passconf', $this->lang->line('field_passconf'), 'required');
            $this->form_validation->set_rules('firstname', $this->lang->line('field_firstname'), 'trim|max_length[50]');
            $this->form_validation->set_rules('lastname', $this->lang->line('field_lastname'), 'trim|max_length[50]');
            
            if (FALSE === $this->form_validation->run())
            {
                $this->load->view('account/create');
                return;
            }
.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases