Welcome Guest, Not a member yet? Register   Sign In
Sharing callback validation rules
#1

[eluser]usefulidiot[/eluser]
Hi,

I have a few controllers that need to use the same callback validation rules. How would I go about this? I tried putting the common callback functions in a library but didn't have much luck ...

Code:
$this->form_validation->set_rules('username', 'Username', "required|callback_{$this->event->validate_test()}");

Please advise.

Thanks
#2

[eluser]umefarooq[/eluser]
better you create your own validation class with using name MY_Formvalidation you have to extend with form_validation class and put this function in that class and no more use callback it will become part of form validation just call your function like required

read Extending Native libraries

http://ellislab.com/codeigniter/user-gui...aries.html
#3

[eluser]Cristian Gilè[/eluser]
If you are using callback_ prefix for your rules the relative function should be place in the same controller where rules are declared.

If you want a more clean solution put the rules in the extended form_validation library.

For example, suppose you want to see if the user is choosing a unique username.

Extend the form validation library:
Code:
<?php
class MY_Form_validation extends CI_Form_validation
{
  
  function __construct()
  {
    parent::__construct();
  }

  function username_check($str)
  {
    //here the code that return TRUE or FALSE
  }

}

Load your form validation:
Code:
$this->load->library('form_validation'); //automatically CI loads your MY_Form_validation

Set the rules:

Code:
$this->form_validation->set_rules('your_field_name','your label','required|xss_clean|username_check');//you don't need callback_ prefix

Cristian Gilè
#4

[eluser]usefulidiot[/eluser]
Thanks for the replies and examples.




Theme © iAndrew 2016 - Forum software by © MyBB