【问题标题】:Codeigniter form_validation helpCodeigniter form_validation 帮助
【发布时间】:2010-07-05 09:39:10
【问题描述】:

您好,我的表单验证有问题,基本上问题是如果验证失败,我会重复加载视图,请查看我的代码 sn-p,

else {
            //the user is a new customer so we need to show them 
            //the card details form
            $this->addViewData('returningCustomer', false);
            $this->load->library('form_validation');
            if($this->input->post('carddetails') == 'Submit') {
                $this->form_validation->set_rules('inpcardtype', 'Card type', 'cardTypeCheck'); //custom callback
                $this->form_validation->set_rules('inpcardnumber', 'Card number', 'required|trim|exact_length[16]');
                $this->form_validation->set_rules('startmonth', 'Start month', 'trim|required');
                $this->form_validation->set_rules('startyear', 'Start year', 'trim|required');
                $this->form_validation->set_rules('endmonth', 'End month', 'trim|required');
                $this->form_validation->set_rules('endyear', 'End year', 'trim|required');
                $this->form_validation->set_rules('inpissuenumber', 'Issue number', 'trim|integer');
                $this->form_validation->set_rules('inpccv', 'CCV Code', 'required|trim|exact_lenght[3]');
                if($this->form_validation->run() == FALSE) {
                    $this->renderTemplate('cv/search/pf_shortlist.php', $this->viewData);
                } else {
                    // validation is passed we need to safe the user details
                    // it is probable that we will need to hash the users card details
                    //@TODO: HASH USER CARD DETAILS LEAVE ONLY THE FINAL DIGITS
                    $this->load->model('checkout_model');
                }
            }
            $this->renderTemplate('cv/search/pf_shortlist.php', $this->viewData);

基本上,如果条件在脚本中进一步失败,则视图会与表单一起加载,然后提交表单,如果验证失败,则将用户传递回表单但显示错误,但原始视图也存在。我可以阻止它这样做吗?

【问题讨论】:

    标签: php codeigniter validation


    【解决方案1】:

    之后

    $this->renderTemplate('cv/search/pf_shortlist.php', $this->viewData);

    你应该做return;,因为在 else 语句之后还有另一个视图加载。#

            //the user is a new customer so we need to show them 
            //the card details form
            $this->addViewData('returningCustomer', false);
            $this->load->library('form_validation');
            if($this->input->post('carddetails') == 'Submit') {
                $this->form_validation->set_rules('inpcardtype', 'Card type', 'cardTypeCheck'); //custom callback
                $this->form_validation->set_rules('inpcardnumber', 'Card number', 'required|trim|exact_length[16]');
                $this->form_validation->set_rules('startmonth', 'Start month', 'trim|required');
                $this->form_validation->set_rules('startyear', 'Start year', 'trim|required');
                $this->form_validation->set_rules('endmonth', 'End month', 'trim|required');
                $this->form_validation->set_rules('endyear', 'End year', 'trim|required');
                $this->form_validation->set_rules('inpissuenumber', 'Issue number', 'trim|integer');
                $this->form_validation->set_rules('inpccv', 'CCV Code', 'required|trim|exact_lenght[3]');
                if($this->form_validation->run() == FALSE) {
                    $this->renderTemplate('cv/search/pf_shortlist.php', $this->viewData);
                    return;// HERE
                } else {
                    // validation is passed we need to safe the user details
                    // it is probable that we will need to hash the users card details
                    //@TODO: HASH USER CARD DETAILS LEAVE ONLY THE FINAL DIGITS
                    $this->load->model('checkout_model');
                }
            }
            $this->renderTemplate('cv/search/pf_shortlist.php', $this->viewData);
            return;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-08-07
      • 2015-10-23
      • 2011-01-04
      • 1970-01-01
      • 2018-06-04
      • 1970-01-01
      • 1970-01-01
      • 2012-07-25
      相关资源
      最近更新 更多