• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
passing custom error message between models and controllers and views.

#1
[eluser]chubbypama[/eluser]
I'm just new to codeigniter... I have a question about how to handle errors.
I have a log in page that I've created, which includes the ability to create a new account. When someone tries to create a new account, the first thing i do is check if the user name
already exists in my database. if it does, I'd like to be able to pass a user friendly error message to the view. (via the controller).

Code:
public function create_member()
{

  //1.  check if the user name already exists.
  $exists = $this->username_exists($this->input->post('username'));
  if ($exists)
  {
  
   return false;
  }
  else
  {
   $new_member_insert_data = array(
    'username' => $this->input->post('username'),
    'password' => md5($this->input->post('password')),
    'user_type' => 'user',
    'first_name' => $this->input->post('first_name'),
    'last_name' => $this->input->post('last_name'),
    'phone' => '1234',
    'email' => $this->input->post('email'),
    'failed_login_attempts' => 0,
    'approved_user' =>0

   );

   $insert = $this->db->insert('users',$new_member_insert_data);
   return $insert; //returns true or false
  }

From what I've read, I can use either config files or session or global variables.
It seems that global vars are a bad thing to do, based on my limited reading. Is this correct? The knee-jerk reaction I have to resolving this problem is to somehow create a global variable and pass that to the controller.


Code:
var $error_message;  //this doesn't actually work.  I still have to figure out why
        public function create_member()
{

  //1.  check if the user name already exists.
  $exists = $this->username_exists($this->input->post('username'));
  if ($exists)
  {
   $error_message = "Username already exists"
   return false;
  }
  else
  {
   $new_member_insert_data = array(
    'username' => $this->input->post('username'),
    'password' => md5($this->input->post('password')),
    'user_type' => 'user',
    'first_name' => $this->input->post('first_name'),
    'last_name' => $this->input->post('last_name'),
    'phone' => '1234',
    'email' => $this->input->post('email'),
    'failed_login_attempts' => 0,
    'approved_user' =>0

   );

   $insert = $this->db->insert('users',$new_member_insert_data);
   return $insert; //returns true or false
  }

public function get_error()
{
  return $error_message;
}


and then in the controller, do something like this:
Code:
$this->load->model('Membership_model');
   if($query = $this->Membership_model->create_member())
   {
    //show display    
    $data['main_content'] = 'signup_successful';
    $this->load->view('includes/template',$data);  
   }
   else
   {
    //problem adding record to database.  show the signup form again
    $data['error_message'] = $this->Membership_model->get_error();
    $data['main_content'] = 'signup_form';
    $this->load->view('includes/template',$data);
   }

I haven't gotten this prototype working yet because it's complaining about my get_error method in the model. It says that the variable $error_message hasn't been declared.
But I would like some feedback / comments about how to handle 'expected' / 'potential' error messages and how to provide /pass the error message to the view.

thanks.

#2
[eluser]Cristian Gilè[/eluser]
You can use form validation and callback. Check the user guide: http://ellislab.com/codeigniter/user-gui...#callbacks

#3
[eluser]chubbypama[/eluser]
sweet! thanks! I'll look into that.


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.