Welcome Guest, Not a member yet? Register   Sign In
Custom callback function in form validation not working
#4

[eluser]alucard[/eluser]
Thanks for replying guys,

I am trying to have form validation code separate from the controller.

This is how I have done it. It may be wrong. Please tell me if it is.

I had to take out the callback keyword when setting the rules for it to work.

The form validation and all callback functions are working correctly except for the 'check_against_plan_type' function.

If I have done it incorrectly please tell me, I tried to do it various other ways but it wouldn't work.

The setting of the rules is done in this class which is in applications/libraries:
Code:
class myform_validation{

    public $my_form;
    public $field_labels = array();
    
    public function  __construct($field_labels = array()) {
        $this->my_form = & get_instance();
        $this->my_form->load->library('form_validation');
        $this->field_labels = $field_labels;
    }
    public function do_validation(){
        if (isset($_POST["submit"])) {
            $this->my_form->form_validation->set_error_delimiters('<div class="error">', '</div>');

            $this->set_rules();
                /*
                    If validation passed return true else return false
                */

            return $this->my_form->form_validation->run()? true: false;
        }
    }

    function set_rules(){
            //set validation rules
            $this->my_form->form_validation->set_rules('category1', $this->field_labels['category1'], "trim|required|xss_clean");
            $this->my_form->form_validation->set_rules('category2', $this->field_labels['category2'], "trim|compare_cat|xss_clean");
            $this->my_form->form_validation->set_rules('title', $this->field_labels['title'], "trim|required|xss_clean");
            $this->my_form->form_validation->set_rules('description', $this->field_labels['description'], "trim|check_against_plan_type|xss_clean");
            $this->my_form->form_validation->set_rules('phone', $this->field_labels['phone'], "trim|required|numeric|min_length[10]|max_length[11]");
            $this->my_form->form_validation->set_rules('link', $this->field_labels['link'], "trim|required|valid_url|xss_clean");
            if(!(isset($_POST['online_only_business'])) && isset($_POST['address_line1'])){
                $this->my_form->form_validation->set_rules('address_line1', $this->field_labels['address_line1'], "trim|required|xss_clean");
            }
            $this->my_form->form_validation->set_rules('address_line2', $this->field_labels['address_line2'], "trim|xss_clean");
            
            if(!(isset($_POST['online_only_business'])) && isset($_POST['city'])){
                $this->my_form->form_validation->set_rules('city', $this->field_labels['city'], "trim|required|xss_clean");
            }
            if(!(isset($_POST['online_only_business'])) && isset($_POST['state'])){
                $this->my_form->form_validation->set_rules('state', $this->field_labels['state'], "trim|required|xss_clean");
            }
            if(!(isset($_POST['online_only_business'])) && isset($_POST['post_code'])){
                $this->my_form->form_validation->set_rules('post_code', $this->field_labels['post_code'], "trim|required|numeric|max_length[4]|min_length[4]|xss_clean");
            }

            $this->my_form->form_validation->set_rules('listing_plan_id', '', "trim|required|xss_clean");
            $this->my_form->form_validation->set_rules('email', $this->field_labels['email'], "trim|required|valid_email|xss_clean");
    }
}

The call back functions are in another class in applications/libraries:
Code:
class my_form_validation extends CI_Form_validation{
    
    public $CI;
    public function _construct(){
        $this->CI = & get_instance();
        $this->CI->load->library('form_validation');
    }
        function compare_cat(){//compare categories - callback function for form validation
            if( !$_POST['category1']=="" &&($_POST['category1'] == $_POST['category2'])){
               $this->CI->form_validation->set_message('compare_cat','Category 1 and category 2 cannot be the same');
                return false;
            }
            else{
                return true;
            }
        }
        function valid_url(){//check if url supplied is valid - callback function for form validation
            if(!preg_match('/^((http:\/\/www\.)|(www\.)|(http:\/\/))[a-zA-Z0-9._-]+\.[a-zA-Z.]{2,5}$/', $_POST['link'])){
                $this->CI->form_validation->set_message('valid_url',"Enter a valid url, make sure to enter 'http://' first.");
                return false;
            }
            else{
                return true;
            }
        }
        function valid_email(){
            if(!preg_match( '/^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/', $_POST['email'])){
                $this->CI->form_validation->set_message('valid_email',"Enter a valid email");
                return false;
            }
            else{
                return true;
            }
        }
        function check_against_plan_type(){
            if((isset($_POST['listing_plan_id']) && $_POST['listing_plan_id'] == 3) && str_word_count($_POST['description']) > 100){
                $this->CI->form_validaton->set_message('check_against_plan_type', "For the plan you have chosen, description word count cannot be more than 100");
                //echo 'more than 100';
                //exit;
                return false;
            }
            else{
                return true;
            }
        }
}
In my controller I have a function which loads:
Code:
$this->load->library('myform_validation', $this->get_field_labels());
  //do somestuff
if($this->myform_validation->do_validation()){
//validation passed
//do somestuff
}


Messages In This Thread
Custom callback function in form validation not working - by El Forum - 05-13-2011, 02:21 AM
Custom callback function in form validation not working - by El Forum - 05-13-2011, 02:30 AM
Custom callback function in form validation not working - by El Forum - 05-13-2011, 02:31 AM
Custom callback function in form validation not working - by El Forum - 05-13-2011, 02:59 AM
Custom callback function in form validation not working - by El Forum - 05-13-2011, 03:09 AM
Custom callback function in form validation not working - by El Forum - 05-13-2011, 03:15 AM
Custom callback function in form validation not working - by El Forum - 05-13-2011, 03:31 AM
Custom callback function in form validation not working - by El Forum - 05-13-2011, 03:48 AM
Custom callback function in form validation not working - by El Forum - 05-13-2011, 03:49 AM
Custom callback function in form validation not working - by El Forum - 05-13-2011, 04:37 AM



Theme © iAndrew 2016 - Forum software by © MyBB