CodeIgniter Forums
extending validation - 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: extending validation (/showthread.php?tid=12015)



extending validation - El Forum - 10-02-2008

[eluser]dimis[/eluser]
I make a
Quote:MY_Validation extends CI_Validation
at application/libraries and I make this function
Code:
function _check_setbox($val) {
       $CI =& get_instance();
        if ( $val !='-1') {
            return TRUE;
        }
        
         $CI->validation->set_message('_check_setbox', 'You must select a value.');
        return FALSE;
    }
Also at my code I write
Code:
$fields['collection']= "collection";
$rules['collection']    = "required|callback__check_setbox($collection)";
But it does not work, all other validation is ok but not this.
What is wrong?
Dimis


extending validation - El Forum - 10-02-2008

[eluser]dimis[/eluser]
I change, I put the function at my controller but it does not work also.
Code:
function _check_setbox($val) {

        if ( $val !='-1') {
            return TRUE;
        }

       $this->validation->set_message('_check_setbox', 'You must select a value.');
        return FALSE;
    }



extending validation - El Forum - 10-02-2008

[eluser]xwero[/eluser]
If you put a rule in the MY_Validation file you can caal it as the CI rules.

You don't have to use the get_instance function as the $CI is a class variable in the Validation library.
Also you don't have to use the set_message function because you can add the message to the language file if you want.


extending validation - El Forum - 10-02-2008

[eluser]dimis[/eluser]
I put at MY_Validation
Code:
function _check_setbox($val) {

       if ( $val !='-1') {
           return TRUE;
       }
else
   {  $this->_error_messages('_check_setbox', 'You must select a value.');
      return FALSE;}
I call it as
Code:
$rules['material']    = "required|_check_setbox($material_id)";
$rules['type']    = "required|_check_setbox($type)";
$rules['collection']    = "required|_check_setbox($collection)";
$fields['collection']= "collection";
$fields['type']= "type";
$fields['material']= "material";
But it does not work


extending validation - El Forum - 10-02-2008

[eluser]xwero[/eluser]
It doesn't work because you add (fieldname) to the rule
Code:
$rules['material']    = "required|_check_setbox";
// function
function _check_setbox($val) {

       if ( $val =='-1') {
           $this->set_message('_check_setbox','You must select a value.');
           return FALSE;
       }

      return TRUE;
}
But to check if a checkbox or radio button is checked or not CI has the isset rule.


extending validation - El Forum - 10-02-2008

[eluser]TheFuzzy0ne[/eluser]
I believe you also need to call on your function by enclosing your arguments within evaluation brackets. Like this:

Code:
$rules['collection']    = "required|callback__check_setbox[$collection]";

It may also be simpler for you to just throw your validation functions into a helper file, then you can load the helper file, and call on the function as if it were a native PHP function, and without having to extend the validation class.