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



Dinamic rule validation - pippuccio76 - 09-26-2022

hi ,sorry for english ,i have several couple of file everyone size_1  quantity_1 size_2
  quantity_2  ecc the field become to a db's table .
how can i set roule dinamicalii , now my roules are 
[CODE]
            $rules=
            [
                                    'idSize_1'=>[
                                                'rules'=>'check_size_quantity[idSize1,idQuantity1]',
                                                'errors'=>
                                                            'check_size_quantity'=>'You must enter both size and quantity of field XXXX '
                                                          ]
                                                ],
                                   
                                             

                                  ];

[/CODE ]

how can i do this dinamically ? how can i set xxx as field name ?


RE: Dinamic rule validation - pippuccio76 - 09-27-2022

this is my $_POST
Code:
Array ( [id_sessioni_ordini] => 3 [nomeCapoAbbigliamento_1] => T-shirt [idCapoAbbigliamento_1] => [taglia_1] => 2 [quantita_1] => 3 [nomeCapoAbbigliamento_2] => Felpa [idCapoAbbigliamento_2] => [taglia_2] => [quantita_2] => [nomeCapoAbbigliamento_3] => Pile [idCapoAbbigliamento_3] => [taglia_3] => 3 [quantita_3] => 2 [nomeCapoAbbigliamento_4] => Pantalone lungo [idCapoAbbigliamento_4] => [taglia_4] => [quantita_4] => )
NOw i can get the max id and write this code  :
Code:
            $post = $this->request->getPost();         


            print_r($post);
           
            //creo una varriabile per vedere l'id più grande
            $max=0;

            foreach ($post as $k=>$v) {

                //salto il controllo sull'id della sessione ordini
                if($k!='id_sessioni_ordini'){
                    //esplodo i post con underscore per recuperare i numeri
                    $array_post =explode('_',$k);

                    //print_r($array_post);
                   
                    //salvo il numero maggiore
                    if($array_post[1]>$max){

                        $max = $array_post[1];
                    }
                }
            }

                       
            //ciclo tutti i post sapendo il num max
            for ($i=1; $i <=$max ; $i++) {

                $nome_campo_rules ='idTaglia_'.$i;
                $nome_taglia = 'taglia_'.$i;
                $nome_quantita = 'quantita'.$i;
                $nome_campo_reale = 'nomeCapoAbbigliamento_'$i;

                $rules[] =[$nome_campo_rules=>[
                                'rules'=>'controllo_taglia_quantita_presenti['.$nome_taglia.','.$nome_quantita.']',
                                'errors'=>[
                                       
                                        'controllo_taglia_quantita_presenti'=>'Devi inserire sia la taglia che la quantita per il campo  '.$post[$nome_campo_reale]
                                        ]
                            ]];
               

            }
Now  how  can i get the value on validation class ?
Code:
    /**
    * Funzione che controlla se è presente sia la taglia che la quantita
    *
*
    */

public function controllo_taglia_quantita_presenti(string $str,string $fields, array $data)
{

$taglia  = $data['taglia_x']; //change x with real number
$quantita = $data['quantita_x']; //change x with real number

if($taglia AND $quantita){

return true;

}else{

return false;
}




}