CodeIgniter Forums
Extending callback function? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: Extending callback function? (/thread-37001.html)



Extending callback function? - El Forum - 12-22-2010

[eluser]Unknown[/eluser]
Hi

In a simple user registration form, it is easy in the create form to prevent a user from entering an email address that is already registered by simply using a call_back function. In the edit form, there is no built in way to do this as callback functions only accept on parameter. :-(

Is there a way to either extend the callback function (ugh!) or insert my own error using my own duplicate checking code directly into the controller?

For example the below works but I cannot trigger an error in form validation:

function edit_email_check($str,$id)
{
// PREVENT EMAIL DUPLICATES
$query = $this->db->query("SELECT * FROM users where email = '$str' AND id != '$id'");
$cnt = $query->num_rows();
if ($cnt)
{
//$this->form_validation->set_message('edit_email_check', 'The %s ' . $str . ' is already in use');
return "$str is already in use";
}
return FALSE;
}

Hope I am making sense ...


Extending callback function? - El Forum - 12-22-2010

[eluser]tonanbarbarian[/eluser]
since the code you have there must be included in the controller, then have your main method in the controller store the id in the object before the validation is done

i.e.
Code:
...
$this->id = $id;
$this->validation->run();
...

Then in your callback just lookup the id
Code:
function edit_email_check($str)
  {
      //  PREVENT EMAIL DUPLICATES
      $query = $this->db->query(“SELECT * FROM users where email = ‘$str’ AND id != ‘{$this->id}’”);
      $cnt = $query->num_rows();
      if ($cnt)
      {
        //$this->form_validation->set_message(‘edit_email_check’, ‘The %s ’ . $str . ’ is already in use’);
        return “$str is already in use”;
      }
      return FALSE;
  }



Extending callback function? - El Forum - 12-22-2010

[eluser]Unknown[/eluser]
Perfect! Doh, Me!

THANK YOU!

Added localizing the var

if($id == NULL){ $id = $this->uri->segment(3); }
$this->id = $id; <---- here then

Final callback function ended up looking like this:

function edit_email_check($str)
{
// PREVENT EMAIL DUPLICATES
$query = $this->db->query("SELECT * FROM users where email = '$str' AND id != '{$this->id}'");
$cnt = $query->num_rows();
if ($cnt)
{
$this->form_validation->set_message('edit_email_check', 'The %s ' . $str . ' is already in use');
return FALSE;
}
return TRUE;
}