Welcome Guest, Not a member yet? Register   Sign In
Validation: Using callbacks from a model
#11

[eluser]TheFuzzy0ne[/eluser]
You might be onto something here. It would be nice to have something like $this->form_validation->set_scope() which would allow you to pass a reference to the object where the form validation library should look for the call backs, or perhaps if the library checked for global functions too, so you could just load your validation callbacks as a helper.
#12

[eluser]KeyStroke[/eluser]
Can someone from the community do this and get it to be somehow included in the framework? or do the people behind CodeIgniter have to do it themselves?
#13

[eluser]TheFuzzy0ne[/eluser]
Well ideally, you should do it yourself, but if you feel it will benefit others, I'm sure they may integrate it into the framework. I'm having a few problems with my development server at the moment, but when I have the fixed, I can get on it for you, as I feel I would also find this useful. Can you give me until tomorrow?
#14

[eluser]KeyStroke[/eluser]
Thanks man, you'd do us all a favor.

Don't rush yourself though, I could personally live with it for a while. Smile
#15

[eluser]pistolPete[/eluser]
Have a look at the wiki: MY Form validation - Callbacks into Models
#16

[eluser]TheFuzzy0ne[/eluser]
Nice one! That's pretty much what I would have done, only I had added a set_scope() function.
#17

[eluser]KeyStroke[/eluser]
Great! thanks pistolPete! I'll see if this one works as advertised. Smile
#18

[eluser]Colin Williams[/eluser]
I just don't like the idea of the Model so directly validating user input. You can always just call a model function from within a callback in the controller. This keeps the roles of the Controller and Model more appropriately separate (it's a matter of context, really). This also makes things more flexible down the road, since certain controllers can extend and alter the validation process by customizing their own callbacks. If you kept it in the model, you might find yourself performing unnecessary logic in the model function just to accommodate the slightly different validation schemes.

DRY isn't always what you need.
#19

[eluser]TheFuzzy0ne[/eluser]
I agree, Colin. I also don't like the idea of calling validation procedure from within a model, and I would never do so, but the having the freedom to place your validation functions into a library, object or anything else that takes your fancy is a huge bonus in my opinion.

The one and only thing I've never liked about CodeIgniter is the restriction of only being able to place your callback function in a controller. It can be called upon from the outside world, and if you prefix it with an underscore, you have to use two underscores in your callback rule, and I think it ruins readability and can make things a little more confusing.

For the short term, I use this line of code at the top of each of my validation callbacks.
Code:
if ( ! isset($this->form_validation)) { show_404(); }

If it's called directly, then the form validation class is not loaded, so a 404 is thrown. The user should be unaware that this function even exists. This only works, however, if you set your default variable values, otherwise you end up with one or more "Missing argument" warnings.
#20

[eluser]KeyStroke[/eluser]
In my opinion, if it works with Libraries, then it's perfectly fine. Though I'm not sure if that's possible.

It does feel a bit "wrong" to place validation rules in Models, but it's way better than repeating a bunch of callback functions in every controller.




Theme © iAndrew 2016 - Forum software by © MyBB