Reputation: 3906
I'm trying to validate a cakephp file upload
The following is the input in my view
<?php echo $this->Form->input('images.', array('type' => 'file', 'multiple', 'label'=>'Upload Images to your gallery')); ?>
This is the html code I get in the browser
<input type="file" required="required" id="ProjectImages" multiple="multiple" name="data[Project][images][]" />
The following is the code in my model for validation
'images[]' => array(
'extension' => array(
'rule' => array(
'extension' => array('jpeg', 'png', 'jpg'),
'message' => 'Please supply valid images'
)
),
'size' => array(
'rule' => array('fileSize', '<=', '2MB'),
'message' => 'Image must be less than 2MB'
)
)
I've also tried to validate with 'image' as the field name but both do not work. The files are uploading correctly but the validation is not working.
Please help. Thank you
Upvotes: 2
Views: 1969
Reputation: 320
Validation rules like mimeType or fileSize don't seem to work on multiple upload Arrays, you have to validate via custom rules:
$validator->add('uploads', [
'fileSizeSum' => [
'rule' => function($value, $context) {
$fileSizeSumLimit = Configure::read('uploads')['max_uploadsize'];
$fileSizeSum = 0;
foreach($value as $key => $val) {
$fileSizeSum += $val['size'];
if( $fileSizeSum > $fileSizeSumLimit || $val['size'] > $fileSizeSumLimit ) {
return false;
}
}
return true;
},
'on' => function ($context) {
return ! empty($context['data']['uploads'][0]['size']);
},
'message' => __('Uploadlimit x MB!'),
],
'extension' => [
'rule' => function($value, $context) {
$supportedFileTypes = Configure::read('uploads')['allowed_mimetypes'];
foreach($value as $key => $val) {
$ext = pathinfo($val['name'], PATHINFO_EXTENSION);
if( ! empty($ext) && ! in_array($ext, $supportedFileTypes) ) {
return false;
}
}
return true;
},
'on' => function ($context) {
return ! empty($context['data']['uploads'][0]['type']);
},
'message' => __('Allowed Mimetypes: '). implode(', ', Configure::read('uploads')['allowed_mimetypes_userrequest']),
]
]);
Upvotes: 0
Reputation: 18600
Try with name images
instead of images[]
and also correct for extension rule see Document Here
'images' => array(
'extension' => array(
'rule' => array(
'extension',array('jpeg', 'png', 'jpg')
),
'message' => 'Please supply valid images'
),
'size' => array(
'rule' => array('fileSize', '<=', '2MB'),
'message' => 'Image must be less than 2MB'
)
)
Upvotes: 1
Reputation: 2884
Please check the thread linked here - Cakephp: Multiple files upload field sets to required automatically
I would suggest that you try to name the validation in the model as images
rather than images[]
.If that still doesn't work, what I would suggest is for you to write a custom beforeSave
function in your model and handle the validation yourself. You can use the following functions to do the validation.
public function isValidImageFile($filename) {
if($this->checkFileUploadedName($filename) &&
$this->checkFileUploadedLength($filename) &&
$this->checkImgFileExtn($filename)) {
return true;
}
return false;
}
private function checkFileUploadedName($filename)
{
return (bool) ((preg_match("`^[-0-9A-Z_\.]+$`i",$filename)) ? true : false);
}
private function checkFileUploadedLength($filename)
{
return (bool) ((mb_strlen($filename,"UTF-8") < 225) ? true : false);
}
private function checkImgFileExtn($filename) {
$file_parts = pathinfo($filename);
$supportedFileTypes = array('jpg', 'png', 'jpeg', 'bmp');
if(in_array(strtolower($file_parts['extension']), $supportedFileTypes)) {
return true;
}
return false;
}
You could another method there to check the file size. You can check filesize using the php - filesize method.
Upvotes: 1