![]() |
Validating in a model - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23) +--- Thread: Validating in a model (/showthread.php?tid=5729) |
Validating in a model - El Forum - 02-01-2008 [eluser]Référencement Google[/eluser] Hi, From a MVC point of view, it seems a correct idea to use a model to validate forms. Has someone already done this? If yes, could you post some code examples on how to do that? Validating in a model - El Forum - 02-01-2008 [eluser]thurting[/eluser] I agree with you and have done this in the past. Let me look through my code and post it for you. Validating in a model - El Forum - 02-01-2008 [eluser]thurting[/eluser] Here you go. The below is a basic example that I scraped from my code. To be honest, I haven't used CI in a while, so the following may be sloppy. I checked it though and it ran well. There are many ways to do it. This is just one. BTW, I had been using a custom validation class (the core one sucks - sorry), but replaced it with the CI version in this example. First thing is to create an abstract class for all models to inherit from (put it in your application/libraries folder): Code: <?php Second thing is to create out model that inherits from this base class (put it in your application/models folder): Code: <?php Third thing is our controller (put it in your application/controllers folder): Code: <?php Notice that the validation instance is instantiated during model construction. This can be altered. In any case, the main part is the _validate() method. The default behavior is to return true. However, as it is protected, each subclass should override this method to provide specific validation behavior. As you can see with the create() method, before a query is run we run a conditional with _validate() and proceed accordingly. All queries through the model should be encapsulated in conditionals within the constructor, so when we encounter an error we can handle it properly. There are plenty of other ways to accomplish this, but the above is the simplest to understand. Validating in a model - El Forum - 02-01-2008 [eluser]Référencement Google[/eluser] That's a very intersting way to do. I will give it some tests right now. Thanks Thurting ! Validating in a model - El Forum - 02-01-2008 [eluser]thurting[/eluser] Enjoy. Use it as a jumping off point. BTW, here is a simple schema you can use to test: create table admins ( id int unsigned not null auto_increment, username char(40) not null, primary key (id) ); Validating in a model - El Forum - 02-02-2008 [eluser]sikkle[/eluser] Let's be honest, this is an approach indeed, but humm not so sure about how this can be structured in application development. i'm still wondering if there any way to create something more "method related" i mean, anyone already make some test to build validation as a model method, no extention, just another method build someway on model ? Validating in a model - El Forum - 02-02-2008 [eluser]wiredesignz[/eluser] I posted nearly a week ago, this change to the validation library allows callbacks into models or libraries. http://ellislab.com/forums/viewthread/69797/ Validating in a model - El Forum - 02-02-2008 [eluser]tomcode[/eluser] edit : sorry, posted in the wrong thread |