Hi I am performing an Ajax image upload performing a number of validations. While the ->isValid method works OK, some of the rules configured in config/validation.php do not. And, as worrying, the validator is returning different errors to the ones configured.
Controler
PHP Code:
Code:
if($this->request->isAJAX()) {
helper(['form', 'url']);
$request = \Config\Services::request();
$validation = \Config\Services::validation();
$get_errors = array();
//get form data
$file = $this->request->getFile('image');
$id = $this->request->getPost('id');
$image_use = $this->request->getPost('image_use');
print_r($file);
echo '
ID IS: '.$id;
echo '
- IMAGE USE IS: '.$image_use;
//check image is valid
if(!$file->isValid($file)) {
$get_errors['errors'][]=array('invalid'=>'File Upload Error'.$file->getErrorString().'-'.$file->getError());
}
The validation rules in config/validation.php
PHP Code:
Code:
public $image_upload = [
'file' => [
'rules' => 'mime_in[file,image/jpg,image/jpeg,image/gif,image/png]|max_size[file,4096]',
'errors' => [
'mime_in' => 'Invalid Image File. Only jpg, jpeg, gif or png allowed',
'max_size' => 'Image Too Large - Max File Size 4Mb'
]
],
'id' => [
'rules' => 'required',
'errors' => [
'required' => 'User Not Defined.'
]
],
'image_use' => [
'rules' => 'required|in_list[Players, Teams, Groups]',
'errors' => [
'required' => 'Image Use type required',
'in_list' => 'Image Use Not Defined'
]
],
'width' => [
'rules' => 'greater_than_equal_to[225]',
'errors' => [
'greater_than_equal_to' => 'Image too small. Min width/height 225px'
]
],
'height' => [
'rules' => 'greater_than_equal_to[225]',
'errors' => [
'greater_than_equal_to' => 'Image too small. Min width/height 225px'
]
],
];
and I make a call to the validation rules like this.
PHP Code:
Code:
//validate form data
if(!$get_errors) {
$validation->reset();
list($width,$height)=getimagesize($file);
$image_mime=$file->getMimeType($file);
echo $image_mime;
$form_validation=array('file'=>$file, 'mime'=>$image_mime, 'id'=>$id, 'image_use'=>$image_use, 'width'=>$width, 'height'=>$height);
$validation->run($form_validation, 'image_upload');
$get_errors['errors'][]=$validation->getErrors();
}
print_r($get_errors);
strangely, the following is returned.
CodeIgniter\HTTP\Files\UploadedFile Object
(
[path:protected] => C:\Windows\Temp\phpCD95.tmp
[originalName:protected] => card cover.png
[name:protected] => card cover.png
[originalMimeType:protected] => image/png
[error:protected] => 0
[hasMoved:protected] =>
[size:protected] => 137244
[pathName:SplFileInfo:private] => C:\Windows\Temp\phpCD95.tmp
[fileName:SplFileInfo:private] => phpCD95.tmp
)
ID IS: 128
- IMAGE USE IS: Player
- MIME TYPE IS: image/png
Array
(
[errors] => Array
(
[0] => Array
(
[file] =>
file does not have a valid mime type.
)
)
)
if the (file) validation rules are reversed to the folowing received. It appear the rule before the pipe is ignored. But still the configured errors are not being returned...
PHP Code:
public $image_upload = [
'file' => [
'rules' => 'max_size[file,4096]|mime_in[file,image/jpg,image/jpeg,image/gif,image/png]',
'errors' => [
'mime_in' => 'Invalid Image File. Only jpg, jpeg, gif or png allowed',
'max_size' => 'Image Too Large - Max File Size 4Mb'
]
],
'id' => [
'rules' => 'required',
'errors' => [
'required' => 'User Not Defined.'
]
],
'image_use' => [
'rules' => 'required|in_list[Players, Teams, Groups]',
'errors' => [
'required' => 'Image Use type required',
'in_list' => 'Image Use Not Defined'
]
],
'width' => [
'rules' => 'greater_than_equal_to[225]',
'errors' => [
'greater_than_equal_to' => 'Image too small. Min width/height 225px'
]
],
'height' => [
'rules' => 'greater_than_equal_to[225]',
'errors' => [
'greater_than_equal_to' => 'Image too small. Min width/height 225px'
]
],
];
Array
(
[errors] => Array
(
[0] => Array
(
[file] => file is too large of a file.
)
)
)
Any ideas????