Welcome Guest, Not a member yet? Register   Sign In
problems validation image
#1

Having trouble with validation of an image upload...

When "kategori" fails validation, it returns the error message...
but when the image upload fail, i get no error message.

so this is the validator in the controller...


PHP Code:
## Validation
        
   $input $this->validate([
            
  'kategori' => 'required|min_length[3]|is_unique[kategori.kategori]',
            
  'image' => 'uploaded[image]|max_size[image, 4]',
            
  ]);
            
//echo "<pre>input: "; print_r($input); echo "</pre>";
        
   if (!$input) {
            
   echo "failed";
            
  return redirect()->to('/admin/cat/create')->withInput()->with('validation',$this->validator); 
        
   } else { 

And this is the form view
PHP Code:
    <?php $validation = \Config\Services::validation(); ?>
    <div class="row">
    <div class="col-md-12">
        <form action="<?=site_url('admin/cat/store')?>" method="post" enctype="multipart/form-data">
        <div class="form-group">
            <label for="kategori">Kategori:</label>
            <input type="text" class="form-control" name="kategori" required value="<?= old('kategori'?>">

            <!-- Error -->
            <?php if( $validation->getError('kategori') ) {?>
            <div class='alert alert-danger mt-2'>
                <?= $error $validation->getError('kategori'); ?>
            </div>
            <?php }?>
        </div>
        <div class="form-group">
            <label for="image">Bilde:</label>
            <input type="file" class="form-control" name="image" value="<?= old('image'?>">

            <!-- Error -->
            <?php if( $validation->getError('image') ) {?>
            <div class='alert alert-danger mt-2'>
                <?= $error $validation->getError('image'); ?>
            </div>
            <?php }?>
        </div>
        

        <button type="submit" class="btn btn-success" name="submit">Lagre</button>
        </form>
    </div> 

I recon i got some error in my codingm, but i can't find it. Any help is appreciated.

Rgrds Bengt
Reply
#2

Read this.

CodeIgniter 4 User Guide - Working with Uploaded Files
What did you Try? What did you Get? What did you Expect?

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

(This post was last modified: 05-26-2021, 10:48 PM by bengtdg.)

(05-26-2021, 09:13 PM)InsiteFX Wrote: Read this.

CodeIgniter 4 User Guide - Working with Uploaded Files

Thank you. I have read there. 

It failes here when validation of image fails...
PHP Code:
if(isset($postData['submit'])){
        
   ## Validation
        
   $input $this->validate([
            
  'kategori' => 'required|min_length[3]|is_unique[kategori.kategori]',
            
  'image' => 'uploaded[image]|max_size[image, 4]',
            
  ]);
            echo 
"<pre>var_dump: \$_POST: "var_dump($_POST); echo "</pre>";
            
            echo 
"<pre>var_dump: \$_FILES: "var_dump($_FILES); echo "</pre>";
        
   if (!$input) {  ////IT FAILS HERE
            
   echo "failed";
            
  //return redirect()->to('/admin/cat/create')->withInput()->with('validation',$this->validator); 
        
   } else { 

som var dump from the var dumps above 
Code:
$request = service('request');
        $postData = $request->getPost();

        echo "<pre>"; print_r($postData); echo "</pre>";


Array
(
    [kategori] => gfdfgdsfg
    [submit] =>
)


var_dump: $_POST:
C:\www\kalori\app\Controllers\CatController.php:40:
array (size=2)
  'kategori' => string 'gfdfgdsfg' (length=9)
  'submit' => string '' (length=0)

var_dump: $_FILES:
C:\www\kalori\app\Controllers\CatController.php:42:
array (size=1)
  'image' =>
    array (size=5)
      'name' => string 'Bengt.jpg' (length=9)
      'type' => string 'image/jpeg' (length=10)
      'tmp_name' => string 'C:\wamp64\tmp\php4934.tmp' (length=25)
      'error' => int 0
      'size' => int 1049248
failed

It just pussles me. If validation of kategori fails, it returns the error message .
But nothing happens when validation of image fails. The validation error never comes back to the form.

If i set max_file to 4096 then the file is successfully uploaded.
Reply
#4

(This post was last modified: 05-27-2021, 05:39 AM by InsiteFX.)

Take a look at the Myth/Auth AuthController and layout views.
Also look at the _message_block view.

Will show you how Lonnie does it using sessions etc;

Also file sizes are set in the php.ini file.


max_post_size and max_file_size they should both be set to the same value.
What did you Try? What did you Get? What did you Expect?

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

PHP Code:
$rules = [
                'image' => 'uploaded[image]|max_size[image,4096]|ext_in[image,png,jpg,gif,webp]',
                'contact_id' => 'required'
            ];
            if (!$this->validate($rules)) {

                return $this->respond([
                    'error' => $this->validator->getErrors(),
                    'success' => false
                
], ResponseInterface::HTTP_NOT_ACCEPTABLElang('Common.api.validation'));

            
Enlightenment  Is  Freedom
Reply




Theme © iAndrew 2016 - Forum software by © MyBB