Welcome Guest, Not a member yet? Register   Sign In
Combine the form_validation config file with rule callback functions
#1

[eluser]cjke[/eluser]
Hi guys,

I'm putting together a registration page. I need to add a custom callback function to validate names (rule:alpha would almost work but need to include ' for names like o'leary).

If I create a set of rules and the call back all in my controller the following works:

Code:
array(
    'field'   => 'first_name',
    'label'   => 'First Name',
    'rules'   => 'required|callback_valid_name'
)

function valid_name($s) {
    // return true or false depending, etc
}

BUT I would like to store it in the form_validation config file. The required etc is fine, but the callback never gets called. I believe the rule can't find the function. However it will work if I put the valid_name function in the controller. So where can I put the custom functions so the config file rule set can find them??

Thanks
#2

[eluser]InsiteFX[/eluser]
This is because the callback has to be in the same file as the Form_Validation!

I usally put my Form_Validation and Forms in a user library, this way I can always
reuse them later in other applications.

InsiteFX
#3

[eluser]cjke[/eluser]
Can I confirm that what you are saying is that this line:

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

And the callback functions, such as:

valid_name($s){}

Need to be in the same controller (or file)?



So with your example of creating a library item. You create a new library, which contains the $this->load->library('form_validation'); and the callbacks. Then your controller brings that whole library in?

Thanks
#4

[eluser]InsiteFX[/eluser]
Yes that is how I do it, took me awhile to figure out why
my callbacks were not working, once I placed them at the
bottom of my library everything worked fine.

library
-- constructor - load any libraries helpers etc

-- methods edit - would hold code to edit using a form

-- callbacks

then I place all callbacks at the bottom or right under
the method that uses them.

InsiteFX
#5

[eluser]cjke[/eluser]
Ok cool - looks good.

Thanks for that, that makes a lot more sense
#6

[eluser]dottedquad[/eluser]
This is good to know! I have been pulling my hair out for the past 3 hours trying to figure out why my callback was not getting called.

-Rich




Theme © iAndrew 2016 - Forum software by © MyBB