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



custom validation dinamic - pippuccio76 - 05-03-2024

hi there is a way to send to parameter to a validation rule ?

Code:
    public function required_if($first_parameter,$second_parameter)
    {

    if(!$first_parameter ){

    return true;

    }else if($first_parameter AND !$second_parameter){
   
    return false;
   
    }else{
   
    return true;
   
    }
   
    }


If is possible how can i pass they in rules ?


RE: custom validation dinamic - ozornick - 05-03-2024

See https://codeigniter4.github.io/userguide/libraries/validation.html#allowing-parameters
Parameters using as "required|required_if[first,second]"


RE: custom validation dinamic - pippuccio76 - 05-03-2024

(05-03-2024, 10:22 AM)ozornick Wrote: See https://codeigniter4.github.io/userguide/libraries/validation.html#allowing-parameters
Parameters using as "required|required_if[first,second]"

my first parameter is a radio button , 0 or 1 value ar always passed but i must check if is 0 or 1 ad check other parameter passed .

I have a form with more than 10 field that must required only if first parameter is 1 .
My idea was send parameter dinamically ,without $data['name_of_first_parameter] and $data[name_of_second_parameter] , because first parameter is always same name but second change for every field and i must create ten custom rules (instead one), it's possible ?


RE: custom validation dinamic - ozornick - 05-04-2024

I didn't understand the question well.
Perhaps for your comparison you need to make parameters for 10 radio buttons.
For example, get_radio_params() (first=>1,second=2,...)
Then use them for the form and for validation? These parameters will be located in $data in the rules


RE: custom validation dinamic - pippuccio76 - 05-06-2024

hi i have 1 radio button :
radio1 (0 or 1) and 20 field.
10 field :
field 11 ,field 12 field 13 , fiel14 , field 15 , field 16 , field 17 , field 18 , field 19 ,field 20
must be required only if radio1 is set to 1 .
With the actual validation i must create 10 custom rules :
-control if radio is 1 anda field 11 is set
-control if radio is 1 anda field 12 is set
-control if radio is 1 anda field 13 is set
......
-control if radio is 1 anda field 20 is set

i would be create only a rules :
i want create one role with 2 parameter :radio values and another parameter
another parameter is the value of field 11,12,13....20

It's possible ?


RE: custom validation dinamic - ozornick - 05-06-2024

I'm telling you, use $data for your rule.
require_if[radio1]
In $data, you will have the rest of the buttons: radio2...radio20
Check their availability at radio1. What is the problem?
PHP Code:
if ($value !== '' && in_array($data'radio2',...'radio20')) { return true; }