CodeIgniter 如何从函数设置验证消息

问题描述 投票:0回答:1

我正在尝试验证信用卡号,到目前为止我能够从中得到结果。但是,每当失败时,表单的验证消息都不会出现。我一直在尝试多种方法,包括设置回调。我不确定我错过了什么。

控制器

public function next(){
        $this->form_validation->set_error_delimiters('<p class="error">', '</p>');
        $this->form_validation->set_rules('inputcardtype','Select Card Type','required|callback_check_default');
        $this->form_validation->set_message('check_default', 'Please select the month of expiration');
        $this->form_validation->set_rules('inputcardnumber', 'Card Number', 'trim|required|xss_clean');
        $this->form_validation->set_rules('inputexpirationdatemonth','Select Month','required|callback_check_default');
        $this->form_validation->set_message('check_default', 'Please select the month of expiration');
        $this->form_validation->set_rules('inputexpirationdateyear','Select Year','required|callback_check_default');
        $this->form_validation->set_message('check_default', 'Please select the year of expiration');
        $this->form_validation->set_rules('inputnameoncard', 'Name on Card', 'trim|required|xss_clean');

        $inputcardnumber = $this->input->post('inputcardnumber');
        $inputcardtype = $this->input->post('inputcardtype');
        // var_dump($this->cardnumber_validation($inputcardnumber,$inputcardtype));
        if($this->form_validation->run()==false||$this->cardnumber_validation($inputcardnumber,$inputcardtype)==FALSE){
            $this->index();
        }else{

        }

    }

    function cardnumber_validation($string = NULL,$cardtype=NULL) {
        $this->load->helper('creditcardvalidation'); 
        if(checkCreditCard ($string, $cardtype, $ccerror, $ccerrortext)) {
            return TRUE;
        } 
        else{
            $this->form_validation->set_message("inputcardnumber", 'Invalid Card Number');
            return FALSE;
        }
    }

    function check_default($post_string){
      return $post_string == '0' ? FALSE : TRUE;
    }
php codeigniter validation credit-card
1个回答
0
投票

所以我发现你可以这样做来将 2 个变量传递到回调中

$this->form_validation->set_rules('inputcardnumber', 'Card Number', 'trim|required|xss_clean|callback_cardnumber_validation['.$this->input->post('inputcardtype').']');
© www.soinside.com 2019 - 2024. All rights reserved.