Welcome Guest, Not a member yet? Register   Sign In
Calculate Age In Controller Using Call Back Function
#1

[eluser]rochellecanale[/eluser]
hello guys I need a little help. My problem is how can I create a callback function in validating age. Here's my controller:

Code:
$this->form_validation->set_rules('username','Username','required|trim|min_length[8]|max_length[15]|is_unique[member.username]');
            $this->form_validation->set_rules('mem_type','Membership Type','required');
            $this->form_validation->set_rules('referral','Reference ID','required');
            $this->form_validation->set_rules('lastname','Last Name','required');
            $this->form_validation->set_rules('firstname','First Name','required');
            $this->form_validation->set_rules('middlename','Middle Name','required');
            $this->form_validation->set_rules('email','Email Address','required|is_unique[member.email]|valid_email');
            $this->form_validation->set_rules('contact','Mobile Number','required|numeric|is_unique[member.mobile_number]');
            $this->form_validation->set_rules('gender','Gender','required');
            $this->form_validation->set_rules('address','Address','required|xss_clean');
            $this->form_validation->set_rules('year','Year','required');
            $this->form_validation->set_rules('month','Month','required');
            $this->form_validation->set_rules('day','Day','required');        
            $this->form_validation->set_rules('password','Password','required|matches[passcon]|min_length[8]|max_length[16]');
            $this->form_validation->set_rules('passcon','Password Confirm','required');
            $this->form_validation->set_rules('tin','Tin','required');
            
            if($this->form_validation->run() == FALSE){
                $this->load->view('sites/addNewMember.php');
                $this->load->view('templates/footer');
            }
            else{
                $this->payment_model->addMemberInfo();
                $this->load->view('sites/success');
            }

How can i validate the age inputted in the field. For example I want to validate if the user is 18 years old and above. If 18 the user may proceed if not it will validate and display message "User must be 18 years old and above to gain a membership!!

All my $this->input->post() is in my model should i call this also in my controller? or should i validate it in the model function? Please give me an idea how to do this. Here;s my code in the model below:
Code:
$year = $this->input->post('year');
                $month = $this->input->post('month');
                $day = $this->input->post('day');
                
                $birthday = $year.'-'.$month.'-'.$day;
                
                $member = array(
                    'member_id' => null,
                    'account_type' => $this->input->post('mem_type'),
                    'username' => strtolower($this->input->post('username')),
                    'account_type' => $this->input->post('mem_type'),
                    'lastname' => ucwords($this->input->post('lastname')),
                    'firstname' => ucwords($this->input->post('firstname')),
                    'middlename' => ucwords($this->input->post('middlename')),
                    'gender' => $this->input->post('gender'),
                    'address' => ucwords($this->input->post('address')),
                    'birthday' => $birthday,
                    'email' => strtolower($this->input->post('email')),
                    'mobile_number' => $this->input->post('contact'),
                    'password' => md5($this->input->post('password')),
                    'tin' => $this->input->post('tin'),
                    'upline' => 'none',
                    'account_created' => date('Y-m-d h:i:s')
                );
              
                    $insert =  $this->db->insert('member',$member);
                    $memberid = mysql_insert_id();
                    $this->session->set_userdata('member_id',$memberid);
                
                    return $insert;
#2

[eluser]boltsabre[/eluser]
It's in the documentation, it's called a validation callback function:
http://ellislab.com/codeigniter/user-gui...#callbacks

Also, controller:
You're loading your a view like
Code:
$this->load->view('sites/addNewMember.php');
Drop the .php, no need for it!

Also, after success you are just loading a new view like this:
Code:
else{
   $this->payment_model->addMemberInfo();
   $this->load->view('sites/success');
}
This is a bad idea, because if the user refreshes the page, or presses F5 you are still in your controller and it will run the entire code block again. Check it and see what happens, you'll get either a whole new entry in your db table (ouch) with the same data and display the success view again, or some validation message if you have unique constraints on any on your inputs (ie, this email address already exists, please enter a unique email) and it will load your addNewMember view again.
Use "redirect()" (to a success controller) when the form has been successfully submitted.

Also, in your model:
Code:
$month = $this->input->post('month');
$day = $this->input->post('day');
$birthday = $year.'-'.$month.'-'.$day;
- I cannot see $year defined anywhere???
- $day = $this->input->post('day'); is just bad coding, when you don't have to assign it to a new variable, DONT!
You could have just as easily coded it like this:
Code:
...
'address' => ucwords($this->input->post('address')),
'birthday' => $year.'-'.$this->input->post('month').'-'.$this->input->post('day');
//depending on $year obviously... like I said, not sure where it is being defined...
This will:
- reduce the amount of code you type, increasing development time.
- also reduce the amount of potential bugs you create in the development process!!!
- reduce your server load because:
- each time you create a new variable it has to be stored in your server memory, increasing the chance of creating server load related problems.
- and it reduces the amount of code the server has to load and run at compile time!

And lastly, you should research some method of checking if your insert/update actually worked! What happens if your mysql server was down when this code ran... it just returns $insert, which could be false if the server was down (thus nothing was actually inserted), and you redirect your user to the success page and they will think they have created an account when in reality it wasn't created!

good luck!
#3

[eluser]rochellecanale[/eluser]
ok i will try that thanks




Theme © iAndrew 2016 - Forum software by © MyBB