CodeIgniter Forums
rules multiple uploads - 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: rules multiple uploads (/showthread.php?tid=75179)

Pages: 1 2


rules multiple uploads - Dedov_Evgeniy - 01-05-2020

Code:
<input name='foto[]' type='file' multiple />

How to specify rules for multiple downloads?

PHP Code:
'foto' => ['label' => 'Images''rules' => 'uploaded[foto]|mime_in[foto,image/png,image/jpeg]'



RE: rules multiple uploads - Dedov_Evgeniy - 01-10-2020

Will someone tell me? Is rule validation supported if the field is an array, for example:
Code:
<input name="id_cat[]" />
<input name="id_cat[]" />
<input name="id_cat[]" />
...



RE: rules multiple uploads - littlej - 01-11-2020

Hello !

I think you skipped this page of the documentation  Rolleyes
[/url]
[url=https://codeigniter4.github.io/userguide/libraries/uploaded_files.html#multiple-files]https://codeigniter4.github.io/userguide/libraries/uploaded_files.html#multiple-files


But maybe it's because this section is not appearing in the documentation's summary at the top of the page. It's a "bug"  Big Grin

Have a good day !


RE: rules multiple uploads - Dedov_Evgeniy - 01-12-2020

(01-11-2020, 01:53 AM)littlej Wrote: Hello !

I think you skipped this page of the documentation  Rolleyes
[/url]
[url=https://codeigniter4.github.io/userguide/libraries/uploaded_files.html#multiple-files]https://codeigniter4.github.io/userguide/libraries/uploaded_files.html#multiple-files


But maybe it's because this section is not appearing in the documentation's summary at the top of the page. It's a "bug"  Big Grin

Have a good day !
To download files, it works, with the rest of the data - no ...


RE: rules multiple uploads - littlej - 01-12-2020

Hello !

I am going to need some more clarifications :-) Your original question is "How to specify rules for multiple uploads ?".

The page I gave you is describing exactly this:
1/ Getting ALL files: $imagefile = $this->request->getFiles()
2/ Looping on files: foreach($imagefile['images'] as $img)
3/ Validating and "saving" the file: if ($img->isValid() && ! $img->hasMoved())

"isValid" refers to this: https://codeigniter4.github.io/userguide/libraries/uploaded_files.html#verify-a-file
And you could apply your own custom rules. Juste check whatever you need to check on "$img".

So... What do you mean when you say "with the rest of the data - no". Which data ? A file ? Do you have code to show in order to help me understand ? Thank you ;-)


RE: rules multiple uploads - Dedov_Evgeniy - 01-12-2020

Sorry, everything is in order with the first question, and having studied the code, I realized that there is no verification of arrays of ordinary data fields (example in # 2), which is sad (. I’ll have to finish it by myself.


RE: rules multiple uploads - littlej - 01-12-2020

Validation of arrays with "ordinary data fields": https://codeigniter4.github.io/userguide/libraries/validation.html#validating-keys-that-are-arrays


RE: rules multiple uploads - Dedov_Evgeniy - 01-12-2020

(01-12-2020, 07:42 AM)littlej Wrote: Validation of arrays with "ordinary data fields": https://codeigniter4.github.io/userguide/libraries/validation.html#validating-keys-that-are-arrays
Yes, thanks, I read it. Here, just show me how to check for int data such data from the form:
Code:
<input name="id_cat[]" />
<input name="id_cat[]" />
<input name="id_cat[]" />
...
PHP Code:
$this->validate([
    'id_cat' => ['label' => 'Error message''rules' => 'required|numeric']
    // ...
]); 
PHP Code:
/**
     * Numeric
     *
     * @param string $str
     *
     * @return boolean
     */
    
public function numeric(string $str null): bool
    
{
        return (bool) 
preg_match('/^[\-+]?[0-9]*\.?[0-9]+$/'$str);
    } 

There will be an error, because the method waits for the string, and the array arrives ...
Error:
Argument 1 passed to CodeIgniter\\Validation\\FormatRules::numeric() must be of the type string or null, array given, called in D:\\OpenServer\\OSPanel\\domains\\ci4.ru\\system\\Validation\\Validation.php on line 279


RE: rules multiple uploads - littlej - 01-12-2020

The form (view)

Code:
<form method="post">
    <input type="text" name="id_cat[1]" />
    <input type="text" name="id_cat[2]" />
    <input type="text" name="id_cat[3]" />
    <br><br>
    <input type="submit" value="Submit">
</form>



The validation (controller)


PHP Code:
$rules = [
    
'id_cat.1' => 'numeric',
    
'id_cat.2' => 'numeric',
    
'id_cat.3' => 'numeric'
];

if (! 
$this->validate($rules))
{
    
print_r('error');
}
else
{
    
print_r('success');



Could this be a solution ?


RE: rules multiple uploads - Dedov_Evgeniy - 01-12-2020

Quote:Could this be a solution ?

I tried it now, the error also crashes ...