I have a model "Attachment" which Belongs to another model (hasMany
Attachment).
In model Attachment I put a validator (from the Cake helpbook) to check
against filetypes. After saveAssociated i would expect an error if uploaded
a .txt file, but the model is save successfully?
*Add.ctp*
for($i=0;$i<1;$i++)
{
?>
<div class="row">
<?php
echo $this->Form->file('Attachment.'.$i.'.submittedfile');
?>
</div>
<?php
}
*Attachment model:*
<?php
App::uses('AppModel', 'Model');
class Attachment extends AppModel {
public $belongsTo = 'CollectionRequest';
public $validate = array(
'image' => array(
'rule' => array(
'extension',
array('gif', 'jpeg', 'png', 'jpg')
),
'message' => 'Please supply a valid image.'
)
);
public function beforeSave($options = array()) {
parent::beforeSave($options);
}
public function beforeValidate($options = array()) {
// ignore empty file - causes issues with form validation when file
is empty and optional
if (!empty($this->data[$this->alias]['error']) &&
$this->data[$this->alias]['error']==4 &&
$this->data[$this->alias]['size']==0) {
unset($this->data[$this->alias]);
}
parent::beforeValidate($options);
}
}
?>
*Controller*
if ($this->CollectionRequest->saveAssociated($this->request->data)) {
// All good
$this->Session->setFlash(__('CollectionRequest saved'));
return $this->redirect(array('action' => 'index'));
}
thanks
--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP
---
You received this message because you are subscribed to the Google Groups
"CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/cake-php.
For more options, visit https://groups.google.com/d/optout.