Welcome Guest, Not a member yet? Register   Sign In
ensure user is 18 years of age - date valdation.
#1

[eluser]Andy78[/eluser]
I'm trying to create a date validation callback function that will ensure the date is older than 18 years from the current date. I'm using the formdate library to generate the date select boxes. It just does not seem to be doing anything with the date validation rules at all. Im not sure if im passing in the day, month and year fields to the callback function correctly? where am i going wrong here?

This is what I have so far

controller
Code:
function signup()
    {
        
        $yearmin = date("Y", strtotime('-18 years'));
        
        $this->load->library('formdate');
        $formdate = new FormDate();
        $formdate->setLocale('nl_BE');
        $formdate->year['start'] = 1930;
        $formdate->year['end'] = $yearmin;
        $formdate->month['values'] = 'numbers';
        $data['formdate'] = $formdate;
        
        $this->load->model('user_model');
        
        $data['country'] = $this->user_model->country_select();        
        
        $data['main_content'] = 'home/registration_page';
        $this->load->view('home/includes/template', $data);
    }
    
    function create_member()
    {
                        
        $this->load->library('form_validation');
        $this->load->library('formdate');
        
        //field name, error message, validation rules
        $this->form_validation->set_rules('name', 'Name', 'trim|required');
        $this->form_validation->set_rules('email', 'Email Address', 'trim|required|valid_email');
        $this->form_validation->set_rules('username', 'Username', 'trim|required|min_length[4]');
        $this->form_validation->set_rules('password', 'Password', 'trim|required|min_length[4]|max_length[32]');
        $this->form_validation->set_rules('password2', 'Password Confirmation', 'trim|required|matches[password]');
        $this->form_validation->set_rules('day', 'trim|required|callback_valid_date');
        $this->form_validation->set_rules('month', 'trim|required|callback_valid_date');
        $this->form_validation->set_rules('year', 'trim|required|callback_valid_date');
        
            
            
        
        if($this->form_validation->run() == FALSE)
        {
            //$this->load->view('home/registration_page', $data);
            $this->signup();
        }
        
        else
        {            
            $this->load->model('user_model');
            
            if($query = $this->membership_model->create_member())
            {
                $data['main_content'] = 'signup_successful';
                $this->load->view('home/includes/template', $data);
            }
            else
            {
                //$this->load->view('/home/registration_page');    
                
                $this->signup();
            }
        }
        
    }
function valid_date()
    {
          $day     = (int)$this->input->post('day');
          $month    = (int)$this->input->post('month');
          $year    = (int)$this->input->post('year');
          
          
          $birthday = mktime(0, 0, 0, $month, $day, $year);
          
        
        if (!checkdate($birthday))
        {
            $this->validation->set_message('valid_date', 'The %s field is invalid.');
            return FALSE;
         }
        
        if ($birthday > strtotime('-18 years')) {
                $this->validation->set_message('validate_birthday', 'You must be 18 years old to sign up.');
                return false;
          }
            
            
          
        
    }

the view:
Code:
echo form_open('login/create_member');

echo form_input('name', set_value('name', 'Name'));
echo form_input('email', set_value('email', 'Email Address'));


echo form_dropdown('country', $country, 0);

?>
<label>Day of Birth</label>
&lt;?php echo $formdate->selectDay();?&gt;    
&lt;?php echo $formdate->selectMonth()?&gt;    
&lt;?php echo $formdate->selectYear()?&gt;    
</fieldset>

<fieldset>
<legend>Login Info</legend>
&lt;?php
echo form_input('username', set_value('username', 'Username'));
echo form_input('password', set_value('password', 'Password'));
echo form_input('password2', 'Password Confirm');

echo form_submit('submit', 'Create Acccount');
?&gt;

&lt;?php echo validation_errors('<p class="error">'); ?&gt;
</fieldset>
#2

[eluser]Mischievous[/eluser]
Don't see you returning true in the valid_date function? if it doesnt get that it passed validation it wont do anything?
#3

[eluser]Andy78[/eluser]
I added the return true but its not making any difference. The problem lies in how im feeding valid_date() with the day, month and year variables but i dont no how to do it correctly

Code:
function valid_date()
    {
          $day     = (int)$this->input->post('day');
          $month    = (int)$this->input->post('month');
          $year    = (int)$this->input->post('year');
          
          
          $birthday = mktime(0, 0, 0, $month, $day, $year);
          
        
        if (!checkdate($birthday))
        {
            $this->validation->set_message('valid_date', 'The %s field is invalid.');
            return FALSE;
         }
        
        if ($birthday > strtotime('-18 years')) {
                $this->validation->set_message('valid_date', 'You must be 18 years old to sign up.');
                return false;
          }
            
            
          return TRUE;
    }
#4

[eluser]danmontgomery[/eluser]
You're mixing validation and form_validation. validation is deprecated, you should only be using form_validation
#5

[eluser]LuckyFella73[/eluser]
Do you get any validation error message? The way you set the
day, month and year via $this->input->post should work.
#6

[eluser]Andy78[/eluser]
[quote author="noctrum" date="1288808732"]You're mixing validation and form_validation. validation is deprecated, you should only be using form_validation[/quote]

Thanks for that! Sometimes you just cant see the wood for the trees, It just takes another set of eyes to the problem. I just somehow missed the form_ in front of validation.

This code now seems to be working well:

Code:
function valid_date()
    {
         $day     = $this->input->post('day');
         $month   = $this->input->post('month');
         $year    = $this->input->post('year');
          
         $birthday = mktime(0, 0, 0, $month, $day, $year);
          
        
        if (!checkdate($month,  $day, $year))
        {
            $this->form_validation->set_message('valid_date', 'The %s field is invalid.');
            return FALSE;
         }
        
        if ($birthday > strtotime('-18 years')) {
                $this->form_validation->set_message('valid_date', 'You must be 18 years old to sign up.');
                return false;
          }
            
            
          return TRUE;
    }

However should I be calling my callback valid_date() on each of the returned date values or is the last enough like so
Code:
$this->form_validation->set_rules('day', 'Day', 'trim|required');
        $this->form_validation->set_rules('month','month', 'trim|required');
        $this->form_validation->set_rules('year','year', 'trim|required|callback_valid_date');



Just want to ensure that this validation is in fact strong enough.




Theme © iAndrew 2016 - Forum software by © MyBB