Welcome Guest, Not a member yet? Register   Sign In
Model method for form validation
#1

[eluser]vertmonkee[/eluser]
When a user submits the registration form I want to check if the email address already exists in the database.

I am trying to do it using a function in my pereson model.

The person_model file looks like this

Code:
<?php

class Person_model extends Model {
    
    function Person_model() {
        parent::Model();
    }
    
    /*
     * Method to check if an email address exists
     * Accepts one parameter an email address
     * Returns false if the email address is already in the database, true if it is not
    */    
    function emailExists($email) {
        
        $this->db->select('EmailAddress');
        $this->db->from('people');
        $this->db->where('EmailAddress', $email);
        
        $email_exists = $this->db->get();
        
        if($email_exists->num_rows() != 0) {
            return FALSE;
        } else {
            return TRUE;
        }
        
    }
    
}

The register controller looks like this

Code:
<?php

class Register extends Controller {

    // -> Bind Main to the Controller.
    function Register() {

        parent::Controller();
        
        //Load the registration model
        $this->load->model("register_model");
        //Load the person model
        $this->load->model("person_model");

    }

    // -> Generate the "index".
    function index() {
        
        //Check if the form has been submitted
        if(isset($_POST['register'])) {
            
            //Set the validation rules
            
            //Required fields
            $rules['firstName'] = "required";
            $rules['surname'] = "required";
            
            //Check for a valid email address
            $rules['email'] = "required|valid_email|".$this->person_model->emailExists($this->input->post("email"));
            
            //Passwords are required and should match
            $rules['password1'] = "required|matches[password2]";
            $rules['password2'] = "required";
            
            //Set the validation rules
            $this->validation->set_rules($rules);
            
            //Check if all fields are valid
            if($this->validation->run() != FALSE) {
                
                //All fields are vlaid so we can add the new user
                //$this->_adduser("data");
                $this->register_model->addUser();
                
            }

        }
        
        $data['title'] = "Registration";
                        
        $data['mainView'] = "register";
        
        $this->load->vars($data);
        
        $this->load->view("template");

    }

}

Entering an email address that exists in the database or one that doesn't makes no difference the validation still allows the user to register.

Is this the correct way of trying to implement this piece of validation?
#2

[eluser]danmontgomery[/eluser]
Code:
$rules['email'] = "required|valid_email|".$this->person_model->emailExists($this->input->post("email"));

Is wrong... You don't pass the function result to the form_validation class, you pass the name of a callback function. I'm not aware of a way you can use a callback in a different class without extending the form_validation library, but someone else might.

[edit]
I suppose you could create a controller function which just returns the result of Person_model::emailExists():

Code:
function callback_emailExists($str) {
  $this->load->model("Person_model");
  return $this->person_model->emailExists($str);
}

http://ellislab.com/codeigniter/user-gui...#callbacks
#3

[eluser]vertmonkee[/eluser]
Thanks for that.

I have implemented it by returning the person_model->emailExists

Worked perfectly.




Theme © iAndrew 2016 - Forum software by © MyBB