If I don’t choose a file, time validation doesn’t work and it doesn’t show the message “invalid file”.
this is action url:
http://localhost/carsdirectory/cars/create_ad
create_ad.ctp(view)
<?php echo $this->Form->create('Car', array('type' => 'file', 'action' => 'create_ad')); ?>
<?php echo $this->Form->file('CarImage.image_path')?>
<?php echo $this->Form->end(array('label' => 'Submit', 'name' => 'Submit', 'div' => array('class' => 'ls-submit')));?>
car_image.php(model)
<?php
class CarImage extends AppModel
{
var $name = 'CarImage';
var $belongsTo = 'Car';
var $validate = array(
'image_path' => array(
'rule' => array('extension', array('jpeg', 'jpg', 'png', 'gif')),
'allowEmpty' => false,
'required' => true,
'message' => 'invaid file'
),
'car_id' => array(
'rule' => "numeric",
'allowEmpty' => false,
'message' => 'Car details could not be saved'
)
);
}
?>
car_images(table)
Field -> 1) id 2) image_path 3)car_id
As I commented and suggested what seems right according to your scheme is that you have these associations:
Car model:
CarImage model:
Field for form, since you can have many files to uploaded (just make a new line with number increment):
To validate and upload files, is easier and more consistent making this in CarImage model (I don’t sure if you needs car_id in $validate variable, if doesn’t work, try remove car_id from $validate):
And your controller needs
saveAllinsteadsave:I hope it helps.