Welcome Guest, Not a member yet? Register   Sign In
Validation Bug??
#1

Hi
There seems to be a problem with form validation (Ci4). IS THIS A BUG ????

I have writtem my validation rules to config/validation.php as per the documentation. The problem is two-fold. 1) Assuming the "Rules for File Upload" interrogate the file header the following do not seem to work at all ( max_size, mime_in, ext_in),  and, 2) the configured error message configured for the rule is not return when the validation fails. Please see the following:

Script
PHP Code:
$file $this->request->getFile('image');  //file upload.jpg  mime = image/jpg, size = 35475  extension = jpg
$id $this->request->getPost('id'); // value = 7

$validation->reset();

$validation_array=array('file'=>$file'id'=>$id);

$validation->run($validation_array'image_upload');
                
$validation_errors=$validation->getErrors(); 

Validation
PHP Code:
public $image_upload = [
        
'file' => [
            
'rules' => 'mime_in[file,image/jpg,image/jpeg,image/gif,image/png]',
            
'errors' => [
                
'mime_in' => 'Invalid Image File. Only jpg, jpeg, gif or png allowed'
            
],
        ],
        
        
'id' => [
            
'rules'  => 'required',
            
'errors' => [
                
'required' => 'User Not Defined.'
            
]
        ]
    ]; 

The above returns {"file":"file does not have a valid mime type.","id":"The id field is required"}
1) There should NOT be errors
2) If there is errors the above should return  {"file":"Invalid Image File. Only jpg, jpeg, gif or png allowed","id":"User Not Defined."}

Now if I run the following validation (to ensure the validator is configured properly)..

PHP Code:
$validation_array=array('file'=>$file'id'=>'7'); 

The above returns {"file":"file does not have a valid mime type."}
If there is errors the above should return  {"file":"Invalid Image File. Only jpg, jpeg, gif or png allowed",}

IS THIS A BUG ??????

Many thanks (in advance).
Reply
#2

Change this:

Code:
public $image_upload = [
        'file' => [
            'rules' => 'mime_in[file,image/jpg,image/jpeg,image/gif,image/png]',

to

PHP Code:
public $image_upload = [
'image' => [
'rules' => 'mine_in[image, ..... 

 Validation checks against the names in your form input (post/get values)  not your variables after it has been submitted. So if your form file input is named "image" which it looks like it is then you have to use "image" in your validation.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB