Welcome Guest, Not a member yet? Register   Sign In
Callbacks
#1

[eluser]aeternuslibertas[/eluser]
Hi How is everyone doing!?

my question is with regard to callbacks...
I was writing an application and wanted to check a user sign up for duplicate emails, just like this thread
http://ellislab.com/forums/viewthread/168921/

my question is... how does the function, email_unique know to take the input value from the email field in the form into the parameter $str
is it because of the
Code:
$this->form_validation->set_rules('email', 'Email', 'required|callback_email_unique');
LINE?
Code:
function email_unique($str)
    {
        // Note that there are 100 of different ways to create this SQL,
        // so do it your own way.
        $result = $this->db->get_where('table_name', array('email' => $str));

        // Is there a row with this email?
        if ($result->num_rows > 0)
        {
            // Let's return false for the validation and set a custom message for this function
            $this->form_validation->set_message('email_unique', 'That email is already used.');
            return FALSE;
        }
        else
        {
            // Everything is good, don't return an error.
            return TRUE;
        }
    }

my approach looks something like this...

Code:
function email_unique()
    {
        //NEW LINE
       $email=$this->input->post('email');
        // Note that there are 100 of different ways to create this SQL,
        // so do it your own way.
        $result = $this->db->get_where('table_name', array('email' => $email));

        // Is there a row with this email?
        if ($result->num_rows > 0)
        {
            // Let's return false for the validation and set a custom message for this function
            $this->form_validation->set_message('email_unique', 'That email is already used.');
            return FALSE;
        }
        else
        {
            // Everything is good, don't return an error.
            return TRUE;
        }
    }

does it make a difference if i keep the
Code:
$str
parameter or use the
Code:
$email=$this->input->post('email');
line?
#2

[eluser]CroNiX[/eluser]
It is doing the same thing internally. In the validation library it checks to see if a callback function exists. If it does, it passes the form value for that field as $str to the callback function. Likewise, if you wanted to add an additional parameter to your callback function, like they do with min_length[length] where length is passed, it will pass anything in brackets as the second parameter to your callback function so you can use it for additional checks. Since its already doing this for you, you might as well use the $str it is already passing. No need to do it a second time.
#3

[eluser]aeternuslibertas[/eluser]
awesome thanks!
before i stumbled upon how to correctly check for duplicate emails, i was using this code, that i wrote, and it wasn't working... i spent hours on it, can u see something that i was missing?

Code:
function check_duplicate_email($email){
        $continue_registration='';
        $q = $this->db->get('realtors');
        
        if($q->num_rows() > 0 ){
            foreach ($q->result() as $row){
                if($email == $row->email){
                    
                    $continue_registration=FALSE;
                    //if $email=$row there is already an exact same email in the system
                    //so this means we stop the registration process
                
                }//end email if
                else if($email!=$row->email){
                    
                    $continue_registration=TRUE;
                    //this line returns true, because the submitted email, does not match an email in the system
                }
            }//end foreach
        }//end if
        return $continue_registration;
    }//end check_duplicate_email
#4

[eluser]cideveloper[/eluser]
first of all there is no reason for you to pull the entire table and then loop through it. That is just bad practice. You should only select things you are looking for.

Code:
$this->db->select('email')->where('email', $email)->get('realtors',1);

secondly your true/false will always return true unless the email address is the last email in the table. See example below

loop 1 - $email!=$row->email - true
loop 2 - $email!=$row->email - true
loop 3 - $email==$row->email - false
loop 4 - $email!=$row->email - true
loop 5 - $email!=$row->email - true

then you return the last. As you can see this will never work unless the email address is the last email in the table.
loop 6 - $email!=$row->email - true
#5

[eluser]Unknown[/eluser]
Excellent post. I want to thank you for this informative read and I appreciate you sharing. Keep up the good work. For your <a href="http://www.thenvs.com">car servicing Ascot</a> needs, please let me know.

More power!




Theme © iAndrew 2016 - Forum software by © MyBB