[eluser]lordmontie[/eluser]
Has anyone ever thought of doing something like this?
Create a new MY_Form_validation.php library file in your application/libraries folder:
Code:
<?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:
Code:
$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
Code:
$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;
}