简体   繁体   English

Codeigniter form_validation帮助

[英]Codeigniter form_validation help

Hi there I am having a problem with my form validation, basically the problem is that I am getting a repeated view loaded if the validation fails, please see my code snippet, 嗨,我的表单验证存在问题,基本上问题是如果验证失败,我将加载一个重复的视图,请参见我的代码段,

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

basically if a condtion fails futher up the script a view is loaded with a form, the form is submitted and if validation fails the user is passed back to the form but is show the errors, however the original view is also present. 基本上,如果条件在脚本中进一步失败,则将视图加载到表单,然后提交表单;如果验证失败,则将用户传递回表单,但显示错误,但是原始视图也存在。 Can I stop it doing this? 我可以停止这样做吗?

After

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

You should do return; 你应该return; as after the else statement theres another view loading.# 在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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM