Welcome Guest, Not a member yet? Register   Sign In
form validation callbacks and helpers
#1

[eluser]ggoforth[/eluser]
Hello,

I have two controllers, both of which take a phone number as input and store it in the database. As part of form validation I'm calling a callback that formats the phone number properly. Which means I have two seperate class methods (one in each controller) to handle the formatting. My question is, can I have the callback method in a helper, so I only have to load it once, or do I need to have the callback method in the controller in which the validation is taking place? Hope that makes sense!

Thanks,

Greg
#2

[eluser]Bramme[/eluser]
What you can do is extend the form_validation library.

Simply create a My_form_validation.php file in your libraries folder (look up the exact things you need to do for extending in the user guide) and place your callback function in that newly created file. Remove 'callback_' from the name though. This should work.
#3

[eluser]ggoforth[/eluser]
I'll give that a shot! THanks Bramme!

Greg
#4

[eluser]TheFuzzy0ne[/eluser]
A method I like to use, is to create the callback in a model or helper, and simply add an alias in the controller:

Code:
#Controller

function my_validation_callback($str="")
{
    return $this->some_model->my_validation_callback($str);
}

The trouble is that when you use helpers, you have to mess around with scope to access CodeIgniter from the outside. It's much easier to make it work if you're callback is within a model or your controller.
#5

[eluser]ggoforth[/eluser]
I Like that approach as well! Thanks FuzzyOne
#6

[eluser]Bramme[/eluser]
I think Fuzzy's approach is better if you'll only need the callbacks in one application (but in multiple controllers). Mine is only recommendable if you're writing new callbacks that you could use over different apps.

Example: I tried making a "unique" callback, that would check a database if the value already existed in the db. But it bugged and I quickly gave up :p
#7

[eluser]Eric Cope[/eluser]
I also extend the form_validation library. I recommend that method for most applications.
#8

[eluser]Thorpe Obazee[/eluser]
I've also extended the Form Validation Library sometimes but I'd probably do that only if it's something I'd think will be generally used throughout the application




Theme © iAndrew 2016 - Forum software by © MyBB