Welcome Guest, Not a member yet? Register   Sign In
New to CI, need help with form validation
#1

[eluser]Brandon Taylor[/eluser]
Hi everyone,

I'm primarily a Django developer, and am having some trouble understanding how to do a couple of things in CI...

1. I have a form that is for a model. I would like to place the form validation in the model, rather than the controller for re-use. I haven't seen an example of how to do this in CI.

2. I need to write a custom validator to verify the uniqueness of email addresses. I'm assuming you can write your own validator functions, I just need to know where to put them, and then how to call it from my model on an insert or update.

I'm pretty pleased with Code Igniter compared to other PHP frameworks I've seen so far. My PHP is fairly rusty, so please bear with me as I get my bearings back.

TIA,
Brandon
#2

[eluser]rdjs[/eluser]
Not really quite sure why you would want to do the form validation in the model. That type of task really should be taken care of in your controller.

For custom validation you can just extend the CodeIgniter validation class:

Code:
class MY_Form_validation extends CI_Form_validation
{
     function MY_Form_validation()
     {        
         parent::CI_Form_validation();
     }

     function check_email()
     {
     }
}
#3

[eluser]wabu[/eluser]
Brandon,

If you're going to try to use the built-in validation check out "Callbacks" under the "Form Validation" section of the docs.

However if you want something closer to what Django and other frameworks offer then you'll probably need to write your own validation. The Form Validation class didn't suit me at all but I found CI to be very well documented and organized with regard to extending the core classes (Model and Controller in my case).

It cost me a little time but I'm much happier with the results.
#4

[eluser]Brandon Taylor[/eluser]
Hi everyone,

Thanks for your replies. I ended up just putting the validation in the controller and adding a callback for the unique email.

I'll have to see if I can extend the CI validator class to do something more OO than I have now.

Cheers,
Brandon




Theme © iAndrew 2016 - Forum software by © MyBB