Outliers
Outliers

Reputation: 175

Codeigniter: Date validation error

Hi i took this code from one of the stackoverflow codeigniter question, and try to fit it into mine

controller:

public function index(){
        $data['months'] = array('FALSE' => 'Month',
                 '1'  => 'Jan',
                 '2'  => 'Feb',
                 '3'  => 'Mar',
                 '4'  => 'Apr',
                 '5'  => 'May',
                 '6'  => 'Jun',
                 '7'  => 'Jul',
                 '8'  => 'Aug',
                 '9'  => 'Sep',
                 '10' => 'Oct',
                 '11' => 'Nov',
                 '12' => 'Dec'
                );
         $data['days']['FALSE'] = 'Day';         
         for($i=1;$i<=31;$i++){
            $data['days'][$i] = $i;
         }

     $start_year = date("Y",mktime(0,0,0,date("m"),date("d"),date("Y")-100));
     $data['years']['FALSE'] = 'Year';

     for ($i=$start_year;$i<=date("Y");++$i) {
        $data['years'][$i] = $i;
     }
    $this->load->view('signup', $data);
}

view:

echo form_label('Select you birthday','birthday')."<br/>".
        form_dropdown('days',$days). " " . form_dropdown('months',$months). " " . form_dropdown('years',$years);

Controller:

public function signup_validation(){
        $this->load->library('form_validation');

        $this->form_validation->set_rules('salutation', 'Salutation', 'required');
        $this->form_validation->set_rules('fName', 'First Name', 'required|trim|alpha|xss_clean|strip_tags');
        $this->form_validation->set_rules('lName', 'Last Name', 'required|trim|alpha|xss_clean|strip_tags');
        if($this->form_validation->run()){
            $month = $this->input->post('months');
            $day = $this->input->post('days');
            $year = $this->input->post('years');
            $birthday = date("d-m-Y",mktime(0,0,0,$month,$day,$year));
        }
        $this->form_validation->set_rules('address', 'Address', 'required|trim|xss_clean|strip_tags|alpha_numeric');
        $this->form_validation->set_rules('contact', 'Contact No', 'required|trim|xss_clean|strip_tags|numeric|min_length[8]|max_length[8]');
        $this->form_validation->set_rules('email', 'Email', 'required|trim|valid_email|is_unique[users.email]|xss_clean|strip_tags');
        $this->form_validation->set_rules('username', 'Username', 'required|trim|alpha_numeric|xss_clean|strip_tags');
        $this->form_validation->set_rules('password', 'Password', 'required|trim|min_length[8]|xss_clean|strip_tags|alpha_numeric');
        $this->form_validation->set_rules('cpassword', 'Confirm Password', 'required|trim|matches[password]|xss_clean|strip_tags|alpha_numeric');

        if ($this->form_validation->run()){
            $key = md5(uniqid());

            $this->load->library('email', array('mailtype'=>'html'));
            $this->load->model('model_users');

            $this->email->from('[email protected]', "SWAP");
            $this->email->to($this->input->post('email'));
            $this->email->subject("Confirm your account.");

            $message = "<p>Thank you for signing up!</p>";
            $message .= "<p><a href='".base_url()."main/register_user/$key'>Click Here</a> to confirm your account</p>";

            $this->email->message($message);

            if ($this->model_users->add_temp_users($key, $birthday)){
                if ($this->email->send()){
                    echo "The email has been sent!";
                } else echo "Could not send the mail";
            } else echo "problem adding to database";

        } else {
            $this->load->view('signup');
        }
    }

Everything works fine, until whenever there is a validation error this will show up: enter image description here

Upvotes: 0

Views: 164

Answers (1)

vignesh.D
vignesh.D

Reputation: 776

If error in validation means just assign empty values for month , day and year OR Set value for month , day and year in at the start of function ...not load it in inside validations...check foreach array with is_array() function ...

Upvotes: 1

Related Questions