• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
rules multiple uploads

#1
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]'
Reply

#2
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[]" />
...
Reply

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

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

#5
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...ify-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 ;-)
Reply

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

#7
Validation of arrays with "ordinary data fields": https://codeigniter4.github.io/userguide...are-arrays
Reply

#8
(01-12-2020, 07:42 AM)littlej Wrote: Validation of arrays with "ordinary data fields": https://codeigniter4.github.io/userguide...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
Reply

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

#10
Quote:Could this be a solution ?

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


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.