Hello, I am making an application with CodeIgniter, which is a form that collects the data and inserts it into the database. What the client asks for is to check that the emails are not repeated, that is, that they are not saved, but that a message appears message that this email is already registered, I'm trying to do it by means of a callback in form validation $this->form_validation->set_rules('email','email','required|valid_email','callback_email');
I leave you the code that I try to do, both for the controller and the model.
controller
public function email($data){
$validation= $this->model->validar_mail($data);
if($validation > 0){
// $this->form_validation->set_message('mails','Se deben aceptar los términos y condiciones');
return false;
}else{
return true;
}
}
model
public function validar_mail($mail){
$correo="select count(*) as numero from cliente where email = $mail";
return $this->db->query($correo)->row()->numero;
}
Fixed code:
driver:
<!-- Validation donde llama al la función del callback -->
$this->form_validation->set_rules('email','email','required|valid_email|callback_verifica');
<!--Funcion del callback -->
public function verifica($mail){
if($this->empleado_model->existe_mail($mail)){
$this->form_validation->set_message('verifica','El email introducido ya esta registrado');
return false;
}else{
return true;
}
}
Model:
public function existe_mail($mail){
$this->db->select('email');
$this->db->from('cliente');
$this->db->where('email', $mail);
$result = $this->db->get('');
if (!$result->num_rows() == 1) {
return false;
}else{
return true;
}
}
You can do it through form validation or you can also do it through a function.
Controller:
Model:
Knowing if the email exists, you no longer register it, if it does not exist you proceed to register.