4

I need to check if the user_id that was inserted exists. I was going to use callbacks in Codeigniter form validation but they require the function that does the validation be in the controller. I'm using this on many pages across multiple controllers so I thought I would do it in a model. Codeigniter doesnt support this though.

http://codeigniter.com/forums/viewthread/205469/

I found this but it seems like a lot of code per validation. I'm using this alot so I dont want that big glob of code every time.

How can I run a form validation from a model with as little code as possible?

Dylan Buth
  • 1,648
  • 5
  • 35
  • 57

1 Answers1

6

Extend CI_Form_Validation. That way you will be able to call your callbacks just like all the other Codeigniter validation functions.

Yan Berk
  • 14,328
  • 9
  • 55
  • 52
  • 1
    Are you saying make a `MY_Form_Validation` ? – Dylan Buth Jun 13 '12 at 17:55
  • This might sound dumb but where would that file go? And would i just have the function that does the validation? or would i need the `$config` stuff too? – Dylan Buth Jun 13 '12 at 18:00
  • Inside `application/core`. Read here: http://codeigniter.com/user_guide/general/core_classes.html – Yan Berk Jun 13 '12 at 18:01
  • Wouldn't the file go in application/libraries ? – Dylan Buth Jun 13 '12 at 18:09
  • No. That was the directory in CI 1.7. – Yan Berk Jun 13 '12 at 18:11
  • And the files inside core are loaded automatically, libraries you have to do it manually. –  Jun 13 '12 at 18:14
  • Just for everyone's FYI, one thing that tripped me up as I tried to extend the form validation library using `MY_Form_validation`, is to get rid of the `callback_` in the `->form_validation->set_rules(...)` since it is no longer a callback within a controller. – tim peterson Apr 01 '13 at 22:05