Welcome Guest, Not a member yet? Register   Sign In
Doubt about custom validation
#1

(This post was last modified: 08-02-2023, 12:00 AM by pippuccio76.)

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 ?
Reply
#2

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

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.
Reply
#4

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
Simple CI 4 project for beginners codeigniter-expenses ( topic )
Reply




Theme © iAndrew 2016 - Forum software by © MyBB