Welcome Guest, Not a member yet? Register   Sign In
Email callback validation using model - for both newbies & profs
#1

[eluser]Unknown[/eluser]
Hi everyone,
Being a newbie I learn better by example and a lot of examples out there have helped me a lot and it is payback time...today I will be posting some easy script in how you can use validation callbacks with models e.g. checking if a user's email exists inorder to send him new password when he forgets his/hers. Let's begin:
***My Database table: callback (2 fields - "id" INT auto increment, "email" varchar(50))

***(1)My view: form.php


<?=form_open('mycontroller/processForm','');?>
&lt;input type="text" name="email" value="" size="30"/&gt;<br/>
&lt;?=$this->validation->error_string; ?&gt;<br/>
&lt;input type="submit" name="submit" value="send" /&gt;
&lt;/form&gt;



***(2)My model(give you only the function/method): (Mymodel)- mymodel.php

function emailExists($my_email)
{
$this->db->where('email', $my_email);

//we check if the email is really found in the table
//by checking if the row found is greater than zero
// The table (callback) in the database has: ID(int, auto increment) and email(varchar) fields

$row = $this->db->get('callback')->num_rows();
if($row > 0)
{

//we got a matching email
return true;
}
else
{

//we don't get a matching email
return false;
}

}




***(3)My controller: mycontroller.php
Place this in the constructor(or if you did alreay e.g. in autoload no need):
$this->load->library('validation');
$this->load->model('mymodel');
$this->load->helper(array('form', 'url'));




//our call back method to check if the give email exists
function checkEmail($email)
{
if($this->Mymodel->emailExists($email)== true)
{
return true;
}
else
{
$this->validation->set_message('checkEmail', 'Sorry, but you are not registered in our system');
return false;
}
}





// then process the form

function processForm()
{
$rules['email'] = 'required | valid_email | callback_checkEmail';
$this->validation->set_rules($rules);

if($this->validation->run() == FALSE)
{
$this->load->view('form');
}
else
{

//you put here what you want to do
//e.g reset the user's password and send it to him/her

}
}




Theme © iAndrew 2016 - Forum software by © MyBB