Codeigniter:日期验证错误

时间:2015-01-27 12:47:59

标签: php codeigniter validation

您好我从stackoverflow codeigniter问题之一中获取此代码,并尝试将其纳入我的

控制器:

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);
}

视图:

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

控制器:

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('admin@snt.website', "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');
        }
    }

一切正常,直到出现验证错误,这将显示: enter image description here

1 个答案:

答案 0 :(得分:1)

如果验证错误意味着只为月,日和年分配空值 要么 在函数开始时设置月,日和年的值...不在内部验证中加载...使用is_array()函数检查foreach数组...