Welcome Guest, Not a member yet? Register   Sign In
Validate multiple input in CI 4
#1
Sad 

Hi,

This is the code in my controller:

PHP Code:
$fields = array(
    "user_id[]" => "required"
);

print_r($this->request->getPost("user_id"));

$final_fields = array();

//to remove the fields if the post request doesn't found
foreach ($fields as $field => $validate) {
    if (!is_null($this->request->getPost($field))) {
        $final_fields[$field] = $validate;
    }
}

if (!
$final_fields) {
    return true;
}

$validate $this->validate($final_fields);

if (!
$validate) {
    $validation = \Config\Services::validation();
    $message $validation->getErrors();
    echo json_encode($message);
    exit();


I'm getting this output:

Code:
Array
(
    [0] => 7
    [1] => 17
)
{"user_id[]":"The user_id[] field is required."}

Looking for help. 
Thanks in advance.
Reply
#2

Well, actually not sure why you have tried such complicated coding but I would go simpler

PHP Code:
$user_id => $this->request->getVar('user_id');

foreach (
$user_id as $ui){
  if($ui == ''){
     $msg 'User id should not be empty';
     return redirect()->to(site_url('/admin/register'))->with('msg'$msg);
  }


Btw, can you write more specifically what was your need and desired result?
Reply
#3

@demyr,

You've used raw code for validation (not CI).

Simply, it's working well with,


PHP Code:
$fields = array(
    "user_id" => "required"
); 


But if there has multiple input with the single field like checkbox, the validation isn't working.

With this code:

PHP Code:
$fields = array(
    "user_id[]" => "required"
); 


It's shouldn't show this error,

Code:
{"user_id[]":"The user_id[] field is required."}


While there has values,


Code:
Array
(
    [0] => 7
    [1] => 17
)
Reply
#4

CodeIgniter 4 User Guide - Running Multiple Validations


permit_empty - Allows the field to receive an empty array, empty string, null or false.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#5

@InsiteFX,

Thanks. But if I add permit_empty and no value found, it won't show error.

I mean, if this code:

PHP Code:
print_r($this->request->getPost("user_id")); 

shows:

Code:
Array ()

Then the validation should show the error what I want.
But after adding permit_empty it won't show error.
Reply
#6

See https://codeigniter4.github.io/CodeIgnit...are-arrays
Reply
#7

@kenjis, Can you please implement it for my case? I don't understand how to make this from your link: https://codeigniter4.github.io/CodeIgnit...are-arrays
Reply
#8

I never tried it, but in your case:
PHP Code:
// The data to test:
'user_id' => [
    0 => 7,
    1 => 17,
]

$validation->setRules([
    'user_id.*' => 'required'
]); 
Reply
#9

@kenjis, it worked. Thank you so much.
Reply
#10

(This post was last modified: 04-03-2023, 12:59 AM by pippuccio76.)

(03-13-2021, 02:15 AM)ciddict Wrote: @kenjis, it worked. Thank you so much.

hi , is there a way to set_value on multiple field? i try to create a for cicle and use value='<?=set_value("serial_number[$i]")?>' but value is empty

edit:
work , for start to 1 array start to 0 now i start for from 0...


How can i validate min_length only if is set the field ( on multiple field)?
this is my rules :

Code:
$rules=
            [
                                    'seriale.*'=>[
                                                'label'=>'Seriale',
                                                'rules'=>'min_length[4]|is_unique[macchina.seriale]|seriale_corretto[seriale]',
                                                'errors'=>[
                                                            'min_length'=>"Lunghezza minima Seriale 4 caratteri", 
                                                            'is_unique'=>'Macchina già presente a magazzino' ,
                                                            'required'=>'Seriale obbligatorio',
                                                            'seriale_corretto'=>'Il Seriale inserito non è un seriale corretto!!!',
                                                          ]
                                                ],
                                    'codice_macchina.*'=>[
                                                'label'=>'Codice macchina',
                                                'rules'=>'min_length[0]|max_length[80]',
                                                'errors'=>[
                                                            'min_length'=>'Lunghezza minima Codice macchina 0 caratteri', 
                                                            'max_length'=>'Lunghezza massima Codice macchina 80 caratteri'
                                                          ]
                                                ],
                                    'id_clienti'=>[
                                                'label'=>'Id clienti',
                                                'rules'=>'min_length[0]|max_length[11]',
                                                'errors'=>[
                                                            'min_length'=>'Lunghezza minima Id clienti 0 caratteri', 
                                                            'max_length'=>'Lunghezza massima Id clienti 11 caratteri'
                                                          ]
                                                ],
                                    'id_documento_arrivo'=>[
                                                'label'=>'Documento arrivo',
                                                'rules'=>'required|max_length[11]',
                                                'errors'=>[
                                                            'min_length'=>'Lunghezza minima Id documento arrivo 0 caratteri', 
                                                            'required'=>'Documento arrivo obbligatorio'
                                                          ]
                                                ],
                                   
                                  ];
But if i leave field empty i have min_length error
Reply




Theme © iAndrew 2016 - Forum software by © MyBB