I have a rule in my model:
array('pdf', 'file', 'types'=>'pdf', 'allowEmpty'=>true, 'wrongType'=>"Only PDF's accepted."),
I would expect this rule to only allow upload of a PDF, however it seems to be ignored and I can upload any file type.
Similarly, if I change ‘types’ to ‘jpg’, I can still upload PDF’s.
Validation is working, and if I remove ‘allowEmpty’ for instance, I get a warning and cannot save the record if I fail to choose a file to upload.
Any ideas?
EDIT:
I figured out my problem…
Because I wanted to save my PDF only after the model was saved, it went something like this:
$model->pdf = CUploadedFile::getInstance($model, 'pdf');
if ($model->save()) {
if (is_object($model->pdf)) {
$model->pdf->saveAs($pdfpath. "/$model->pdf");
}
So the validation was already done at $model->save(). Then I saved the file.
I needed to re-validate after saving the model, to check the file type like this:
$model->pdf = CUploadedFile::getInstance($model, 'pdf');
if ($model->save()) {
if (is_object($model->pdf)) {
if ($model->validate()) {
$model->pdf->saveAs($pdfpath. "/$model->pdf");
}
}
EDIT2:
Actually, I’m still sure that validation should have worked at $model->save(), with the line I have above it, but it doesn’t.