CodeIgniter Forums
Can somebody tell me where I'm going wrong with "callback_" ? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: Can somebody tell me where I'm going wrong with "callback_" ? (/thread-23640.html)



Can somebody tell me where I'm going wrong with "callback_" ? - El Forum - 10-17-2009

[eluser]Jason W[/eluser]
Hi guys.
I'm trying to write a method/function that will validate a users age, but I'm having trouble grabbing the concept of when and how to use "callback_", and I believe that is where my problem is with this function. I'm very new to codeignitor and kinda new to OOP, so go easy on me.
I've pasted the code for my controller below, I hope someone can help. Thanks in advance.


Here's my controller code:
Code:
<?php

class Age extends Controller {
    
    function Age() {

        parent::Controller();        

        $this->load->helper("url");

        $this->load->helper("form");

        $this->load->library('form_validation');

    }
    
    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");
        }
    }    
}
    
?&gt;



Can somebody tell me where I'm going wrong with "callback_" ? - El Forum - 10-17-2009

[eluser]Jason W[/eluser]
And heres my view file:
Code:
&lt;?php echo form_open("age"); ?&gt;
<table id="signupTable">

    <tr>
        <td class="title">Date of Birth:</td>
        <td class="field_cell">

            &lt;!-- DOB DAY --&gt;

            <select name="dob_d">

                <option value="">Day</option>

                &lt;?php                            

                    $day_array = array("01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31");

                    foreach($day_array as $key => $day){                        

                        if($day == $dob_d) {

                            printf(

                                "<option value=\"%s\" selected>%s</option>\n",                                    

                                $day,

                                $day

                            );

                        }

                        else {

                            printf(

                                "<option value=\"%s\">%s</option>\n",                                    

                                $day,

                                $day

                            );

                        }

                    }

                ?&gt;

            </select> -

            

            &lt;!-- DOB MONTH --&gt;

            <select name="dob_m">

                <option value="">Month</option>                        

                &lt;?php                            

                    $month_array = array("Jan" => "01", "Feb" => "02", "Mar" => "03", "Apr" => "04", "May" => "05", "Jun" => "06", "Jul" => "07", "Aug" => "08", "Sep" => "09", "Oct" => "10", "Nov" => "11", "Dec" => "12");

                    foreach( $month_array as $key => $month){                        

                        if($month == $dob_m) {

                            printf(

                                "<option value=\"%s\" selected>%s</option>\n",                                    

                                $month,

                                $key

                            );

                        }

                        else {

                            printf(

                                "<option value=\"%s\">%s</option>\n",                                    

                                $month,

                                $key

                            );

                        }                            

                    }

                ?&gt;

            </select> -        

            

            &lt;!-- DOB YEAR --&gt;

            <select name="dob_y"">

                <option value="">Year</option>

                &lt;?php

                    $current_year = date("Y");

                    $start_year = $current_year - 100;
                    echo $current_year." - ".$start_year;

                    while($current_year >= $start_year) {                        

                        if($current_year == $dob_y) {

                            printf(

                                "<option value=\"%s\" selected>%s</option>\n",                                    

                                $current_year,

                                $current_year

                            );

                        }

                        else {

                            printf(

                                "<option value=\"%s\">%s</option>\n",                                    

                                $current_year,

                                $current_year

                            );

                        }    

                        $current_year--;

                    }

                ?&gt;

            </select>
            &lt;?php echo form_error('dob_d'); ?&gt;
            &lt;?php echo form_error('dob_m'); ?&gt;
            &lt;?php echo form_error('dob_y'); ?&gt;
            &lt;?php echo form_error('age_check'); ?&gt;

        </td>
        <td class="required">*</td>

    </tr>    

    <tr>

        <td colspan="3">

            &lt;?php echo form_hidden("dobcheck")."\n"; ?&gt;

            &lt;?php echo form_hidden("type", " p")."\n"; ?&gt;

            &lt;?php echo form_hidden("submitform")."\n"; ?&gt;

            &lt;input type="submit" value="Signup Now" /&gt;

        </td>

    </tr>
</table>



Can somebody tell me where I'm going wrong with "callback_" ? - El Forum - 10-17-2009

[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";
        }
    }



Can somebody tell me where I'm going wrong with "callback_" ? - El Forum - 10-17-2009

[eluser]Jason W[/eluser]
Thanks for your help dude.
I think I kinda get how it works now, but I still cant seem to get it to work. I saw some buggy coding in the examples I pasted aswell, but I think I've tweaked them all now and I still can't get results I'm looking for. To be honest, I'm thinking about taking the age validation out. I don't know if frameworks are for me lol


Thanks again though dude, your help was a big eye-opener.


Can somebody tell me where I'm going wrong with "callback_" ? - El Forum - 10-17-2009

[eluser]bretticus[/eluser]
Actually, it appears you may be new enough that "drinking the proverbial Kool Aid" of OOP is a just bit too foreign for you. That's all. I suggest learning the basics of OOP. All PHP coders eventually head in that direction sooner or later. Smile Once you get that under your belt, this framework won't seem too daunting.

2 things about your _age_check method.

Code:
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"; //must return TRUE or FALSE (not text)
                return FALSE;
            }
        }

See comments in code above.

Also, in your view I see no call to actually print out the errors that the validation lib may find.

Good luck!

P.S. This is a good series (Allbeit a little boring to watch.)