4

I have checked all the questions of similar kind, and none of them are solving my problem, using CI 2.1.3, and HMVC from Wiredesignz.

I have in my form_validation.php config file the following rule:

array(
    'field' => 'eta-renpal-1',
    'label' => 'Renpal number (1)',
    'rules' => 'required|callback_check_eta_group'
),

And in my ETA controller, I have this function (currently set to ALWAYS be invalid while testing):

public function check_eta_group($reference)
{
    // Internal function for use by form validation system to check if the ETA group requirements are met.
    $this->form_validation->set_message('check_eta_group', 'Other values in the group ' . $reference . ' are also required.');
    return false;
}

For some reason, the "required" function works, but the callback does not. I have tried all other similar suggested solutions, and can't get them to work. Please help?

Edit: The callback does not appear to be called at all. I even did var_dump() in the callback to see if there is output on the screen - none...

Edit2:: See last comment by myself - using that work-around solves the problem, but it is not exactly what I wanted. So - if you have a better solution, please share :-)

6
  • What do you mean by "the callback function does not work"? It isn't applied without error message? Is there an error message? which one if any?... Commented Nov 8, 2013 at 10:12
  • Sorry - the callback does not appear to be called at all. I will edit question. Commented Nov 8, 2013 at 10:12
  • In the doc (ellislab.com/codeigniter%20/user-guide/libraries/…) I couldn't find any example of validation rule combining both 'required' and a callback. If you only put the callback function in 'rules', is the callback function called? Commented Nov 8, 2013 at 10:16
  • 1
    Using a workaround explained here, stackoverflow.com/questions/3029717/…, it works. It is not the way I want it to work with callbacks, but as long as it works, it is probably alright. Thanks anyways. Commented Nov 8, 2013 at 10:24
  • 1
    You're welcome. Glad you've finally solved the problem by yourself, even if not in an 'ideal' way. Commented Nov 8, 2013 at 10:28

2 Answers 2

2

See my last comment under the question

(Using a workaround explained here, stackoverflow.com/questions/3029717/…, it works. It is not the way I want it to work with callbacks, but as long as it works, it is probably alright. Thanks anyways.)

Thanks Frosty for your comments.

2
  • I'm not sure this is an answer to our question... More of an "Here's a way to hack the library and not do it in the expected way" Commented Aug 6, 2014 at 6:51
  • @ChristoKiwi - the work-around is a hack, I know, but since it is an answer to my own question, and not an answer to you, and since there seems no "real" solution for this problem, the answer is fine as it stands. It served the purpose for me, and will continue to do so unless another person gets a better answer for me :-) Commented Aug 6, 2014 at 15:39
0

make sure your function checks are inside the same controller that you are actually running the custom checks in (i.e. it should be able to be called with self::check_eta_group)

I had trouble getting validation working with my checks inside of MY_Controller for instance. But when i moved them into the extended controller it worked fine.

here are two checks and how I called them (all within the same controller)

// custom form validators for datepicker and timepicker
public function date_valid($date){
    $month = (int) substr($date, 0, 2);
    $day = (int) substr($date, 3, 2);
    $year = (int) substr($date, 6, 4);

    $this->form_validation->set_message('date_valid', 'The %s field is not a valid date');
    return checkdate($month, $day, $year);
}

public function time_valid($time){
    $this->form_validation->set_message('time_valid', 'The %s field is not a valid time');      
    if (preg_match("/^(1[0-2]|0?[1-9]):[0-5][0-9] (AM|PM)$/i", $time)) {
        return TRUE;
    } else {
        return FALSE;
    }
}


public function create_custom(){

    // load models and libraries
    $this->load->helper(array('form', 'url'));      
    $this->load->library('form_validation');

    // set form validation rules
    $this->form_validation->set_rules('schedule_date', 'Schedule Date', 'required|callback_date_valid');
    $this->form_validation->set_rules('schedule_time', 'Schedule Time', 'required|callback_time_valid');

....

    if ($this->form_validation->run() == FALSE) { // failed validation
        error_log("validation_errors: ".validation_errors());
    }
0

Not the answer you're looking for? Browse other questions tagged or ask your own question.