• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
extending validation

#1
[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

#2
[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;
    }

#3
[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.

#4
[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

#5
[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.

#6
[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.


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.