CodeIgniter Forums
Doubt about custom validation - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30)
+--- Thread: Doubt about custom validation (/showthread.php?tid=88169)



Doubt about custom validation - pippuccio76 - 08-01-2023

hi , i have a doubt about custom validation 

in controller the rule are :
Code:
my_validation_function[field_1,field_2]
and in validation class :
Code:
    public function my_validation_function(string $str,string $fields, array $data)
    {
        $mymodel = new MyModel();
      
        $res = $mymodel->where('field_1',$data['field1'])
                       ->where('field_2',$data['field_2])
                       ->first();
          
        if(!res){

            return false;

        }else{


            return true;
        }
       

    }
Why must I insert in controller the fields if in validation class there is every post variable ?


RE: Doubt about custom validation - sammyskills - 08-02-2023

If your custom validation rule does not require parameters, you do not need to add any. See the "even" rule in the docs.
For your function/method, you should make the fields arg nullable by adding a ? like so:
PHP Code:
public function my_validation_function(string $str, ?string $fields, array $data) : bool 
{



With that, you should be able to use the $data array from the sent form/user input.


RE: Doubt about custom validation - pippuccio76 - 08-02-2023

But if i must pass a variable to the function i must insert One , tought the variabile are not the variable that i Need. Because in Array $data there are are post variable.


RE: Doubt about custom validation - ozornick - 08-02-2023

You can not specify anything in the rules. See how "alpha" "alpha_numeric" are created
$data is an additional, not the main source of checks