Welcome Guest, Not a member yet? Register   Sign In
Extending callback function?
#1

[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 ...
#2

[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;
  }
#3

[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;
}




Theme © iAndrew 2016 - Forum software by © MyBB