Welcome Guest, Not a member yet? Register   Sign In
SOLVED: Callbacks and form_validation.php
#1

[eluser]gigas10[/eluser]
Hey there,

I'm having a bit of trouble understanding how callbacks work when you are using the form_validation.php file.

Currently this is what I have in my form_validation.php file (at least the important part):
Code:
array(
         'field'   => 'lot_number',
         'label'   => 'Lot Number',
         'rules'   => 'trim|required|xss_clean|prep_for_form|callback_validLotNumber'
    ),

and this is at the bottom of the form_validation.php file:
Code:
function validLotNumber($field){
    if(!preg_match("/^([a-z]{4}&[0-9]{1,100})&/", strtolower($field))){
        $this->form_validation->set_message('lot_number', 'The Lot Number field did not match the pattern 4 letters followed by any amount of integers');
        return false;
    }
    return true;
}

First of all, does my callback function belong in the form_validation.php file? And if it does not, where does it go? Thanks for the help.

My code is not currently working, everything but the callback works. Maybe I'm doing something wrong with the regex? Maybe you have to have some special thing enabled in php to use preg_match? I'm unsure with regular expressions completely, so any help is much appreciated.
#2

[eluser]flaky[/eluser]
the callback function must be in the controller not in the form_validation.php
#3

[eluser]gigas10[/eluser]
Hmm okay well now it's working. However I get this message:

Unable to access an error message corresponding to your field name.

Instead of the message I would like:

The Lot Number field did not match the pattern 4 letters followed by any amount of integers

What's going on here?
#4

[eluser]gigas10[/eluser]
Solved:

Code:
function validLotNumber($field){
    if(!preg_match("/^([a-z]{4}&[0-9]{1,100})&/", strtolower($field))){
        $this->form_validation->set_message('validLotNumber', 'The Lot Number field did not match the pattern 4 letters followed by any amount of integers');
        return false;
    }
    return true;
}

When setting the message must call the method not the field name.




Theme © iAndrew 2016 - Forum software by © MyBB