I'm trying to make a photomanager in cakephp. Everything works as I expect it to do besides one thing. I just don't seem to be able to validate multiple files properly.
Im using a model called Image where I validate the data from my form. This is the form in my view
<?php
echo $this->Form->create('Image', array(
'type' => 'file',
'url' => array_merge(array('action' => 'index'), $this->params['pass']),
'inputDefaults' => array(
'div' => 'form-group',
'wrapInput' => false,
'label' => false,
'class' => 'form-control'
),
'class' => 'well form-inline'
));
echo $this->Form->input('Image.image_manager.', array('type' => 'file', 'multiple'));
echo $this->Form->input('field', array(
'options' => $imageData['buildings'],
'empty' => '(choose one)',
'class' => 'form-control buildingSelector'
));
echo '<div class="flatForm">';
echo $this->Form->input('flat', array(
'options' => '',
'empty' => '(choose one)',
'class' => 'form-control uploadFlat',
'id' => "success"
));
echo '</div>';
echo $this->Form->submit(__('Submit'), array(
'div' => 'form-group',
'class' => 'btn btn-default pull-left'
));
echo $this->Form->end();
?>
This is the model Image where I validate everything:
public $useTable = false;
public $validate = array(
'image_manager' => array(
'uploadError' => array(
'rule' => 'uploadError',
'message' => 'Something went wrong with the file upload',
'required' => TRUE,
'allowEmpty' => FALSE,
),
'photoSize' => array(
'rule' => array('fileSize','<=','2MB'),
'message' => 'Photo size must be less then 2MB.',
'required' => TRUE,
'allowEmpty' => FALSE,
),
'mimeType' => array(
'rule' => array('mimeType', array('image/gif','image/png','image/jpg','image/jpeg')),
'message' => 'Invalid file, only images allowed',
'required' => TRUE,
'allowEmpty' => FALSE
),
'processUpload' => array(
'rule' => 'processUpload',
'message' => 'Something went wrong processing your file',
'required' => TRUE,
'allowEmpty' => FALSE,
'last' => TRUE,
),
'type' => array()
),
);
And this is how I validate in the controller:
$this->Image->set($this->request->data['Image']['image_manager']);
if($this->Image->saveAll($this->request->data['Image']['image_manager'], array('validate' => 'only')))
{
//do something
}else{
echo '<pre>';
debug($this->Image->validationErrors);
echo '</pre>';
}
The thing is, when I try to validate it returns met a false + the error message of the first validation where required is set on true.
So let's say the required of uploadError is set on false and the required of filesize is set on true I will get a false back + the error message that is defined with filesize.
When all required are set on false the uploader works as intented but I am able to upload other files, files that are to big etc...
Now I do am fairly new to cakephp so it might be something stupid I'm missing. Perhaps one of you guys has an idea how this could validate properly. Thanks in advance.
EDIT This is an example of what image_manager contains when trying to upload files.
Array
(
[0] => Array
(
[name] => Hydrangeas.jpg
[type] => image/jpeg
[tmp_name] => /tmp/phpWfut6N
[error] => 0
[size] => 595284
)
[1] => Array
(
[name] => Jellyfish.jpg
[type] => image/jpeg
[tmp_name] => /tmp/phpLmxQtx
[error] => 0
[size] => 775702
)
[2] => Array
(
[name] => Tulips.jpg
[type] => image/jpeg
[tmp_name] => /tmp/phpi69Hsl
[error] => 0
[size] => 620888
)
)
SECOND EDIT ndm his answer was correct, after reformatting my data this is what the array looks like that I send to the saveAll function
Array
(
[0] => Array
(
[image_manager] => Array
(
[name] => Hydrangeas.jpg
[type] => image/jpeg
[tmp_name] => /tmp/phpWfut6N
[error] => 0
[size] => 595284
)
)
[1] => Array
(
[image_manager] => Array
(
[name] => Jellyfish.jpg
[type] => image/jpeg
[tmp_name] => /tmp/phpLmxQtx
[error] => 0
[size] => 775702
)
)
[2] => Array
(
[image_manager] => Array
(
[name] => Tulips.jpg
[type] => image/jpeg
[tmp_name] => /tmp/phpi69Hsl
[error] => 0
[size] => 620888
)
)
)
...data['Image']['image_manager']
actually contains, you are probably passing the data in the wrong format. See Model::saveMany() for the right format. - ndmdata['Image']['image_manager']
I don't immediately see a mistake on my first glanses - BigalowImage
model? iefield
andflat
do not actually belong to theImage
model, but rather theImage
model is associated to whatever model these fields belong to in axxx hasMany Image
association? - ndm