1

I'm trying to validate credit card numbers and so far I am able to get a result from it. However, whenever it fails, the validation message for the form won't turn up. I've been trying several methods including setting a callback. I'm not sure what I'm missing. Hope someone can take a look and help me.

Controller

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;
    }
JianYA
  • 2,750
  • 8
  • 60
  • 136

1 Answers1

0

So I found out you can do this to pass 2 variables into a callback

$this->form_validation->set_rules('inputcardnumber', 'Card Number', 'trim|required|xss_clean|callback_cardnumber_validation['.$this->input->post('inputcardtype').']');
JianYA
  • 2,750
  • 8
  • 60
  • 136