Welcome Guest, Not a member yet? Register   Sign In
Can somebody tell me where I'm going wrong with "callback_" ?
#3

[eluser]bretticus[/eluser]
Yeah, it would seem you have a function in a function. Probably throws an error. Smile

Code:
function index() {
        // SET RULES
        $this->form_validation->set_rules('dob_d', 'day of your birth', 'required');
        $this->form_validation->set_rules('dob_m', 'month of your birth', 'required');
        $this->form_validation->set_rules('dob_y', 'year of your birth', 'required');
        $this->form_validation->set_rules('dobcheck', 'dob check', 'callback_age_check');

        
        // FORM ERROR MESSAGES
        $this->form_validation->set_message('required', 'Please enter the %s.');    
        
        // AGE CHECK (for my "callback_" thingy)
        function age_check() {            
            $d = $this->input->post('dob_d');
            $m = $this->input->post('dob_m');
            $y = $this->input->post('dob_y');
            if($d != "" && $m != "" && $y != "") { // If date fields are not empty, then proceed
                // PREP DATE
                $dob = date("M", $m)." ".$d.", ".$y;

                // PROCEED WITH VALIDATION
                $then = strtotime($dob); // Timestamp from DOB
                $min = strtotime('+16 years', $then); // DOB Timestamp + 16 years
                if(time() < $min) {
                    $this->validation->set_message('age_check', 'The %s field can not be the word "test"');
                    return FALSE; // Under 16;
                } else {
                    $this->validation->set_message('age_check', 'The %s field can not be the word "test"');
                    return TRUE; // Over 16;
                }    
            } else {
                return "Date of Birth is incomplete";
            }
        }
        
        // LOAD VIEW
        if ($this->form_validation->run() == FALSE) {
            $this->load->view("signup/register");
        } else {
            $this->load->view("signup/success");
        }
    }

Your callback method (method is a function in a class) cannot be in another method. I like to name callbacks by popping an underscore in front. CI automatically makes those methods unroutable. Try this:

Code:
function index() {
        // SET RULES
        $this->form_validation->set_rules('dob_d', 'day of your birth', 'required');
        $this->form_validation->set_rules('dob_m', 'month of your birth', 'required');
        $this->form_validation->set_rules('dob_y', 'year of your birth', 'required');
        $this->form_validation->set_rules('dobcheck', 'dob check', 'callback__age_check');

        
        // FORM ERROR MESSAGES
        $this->form_validation->set_message('required', 'Please enter the %s.');    
        
            
        
        // LOAD VIEW
        if ($this->form_validation->run() == FALSE) {
            $this->load->view("signup/register");
        } else {
            $this->load->view("signup/success");
        }
    }  
    
    // AGE CHECK (for my "callback_" thingy)  
    function _age_check() {            
        $d = $this->input->post('dob_d');
        $m = $this->input->post('dob_m');
        $y = $this->input->post('dob_y');
        if($d != "" && $m != "" && $y != "") { // If date fields are not empty, then proceed
            // PREP DATE
            $dob = date("M", $m)." ".$d.", ".$y;

            // PROCEED WITH VALIDATION
            $then = strtotime($dob); // Timestamp from DOB
            $min = strtotime('+16 years', $then); // DOB Timestamp + 16 years
            if(time() < $min) {
                $this->validation->set_message('age_check', 'The %s field can not be the word "test"');
                return FALSE; // Under 16;
            } else {
                $this->validation->set_message('age_check', 'The %s field can not be the word "test"');
                return TRUE; // Over 16;
            }    
        } else {
            return "Date of Birth is incomplete";
        }
    }


Messages In This Thread
Can somebody tell me where I'm going wrong with "callback_" ? - by El Forum - 10-17-2009, 05:02 PM



Theme © iAndrew 2016 - Forum software by © MyBB