Welcome Guest, Not a member yet? Register   Sign In
loading a model from within a custom library or helper file
#5

[eluser]tonanbarbarian[/eluser]
Personally I dont really have much of a problem loading models in my libraries
But here is something else to consider.
If you use a callback in the standard validation library you can still check for unique usernames or email addresses

You need to create the callback function in the controller, or extend the validation class and put it there.
Then your callback function could call your model and get it to do the validation

sample code in method of controller
Code:
$this->load->model('user');
// get validation rules from model
$this->validation->set_rules($this->user->validationRules());
...
function unique_username($username) {
  $this->load->model('user');
  return $this->user->unique_username($username);
} // unique_username()

function unique_email($email) {
  $this->load->model('user');
  return $this->user->unique_email($email);
} // unique_email()

Now in your model
Code:
function validationRules() {
  return array(
    'username'=>'trim|required|callback_unique_username',
    'email'=>'trim|required|callback_unique_email'
  );
} // validationRules()

function unique_username($str) {
  // code to determine if the username is unique
  ...
  return (bool) $result;
} // unique_username()

function unique_email($str) {
  // code to determine if the email is unique
  ...
  return (bool) $result;
} // unique_email()

Now what I havent included in the code is that to properly determine if the username and email are unique you will need to know the id of the current user so that your queries are able to filter out the current record.
To do this simply set a property of the user model that is the id before the validation->run

Anyway this is a start...


Messages In This Thread
loading a model from within a custom library or helper file - by El Forum - 12-15-2007, 05:24 PM



Theme © iAndrew 2016 - Forum software by © MyBB