Filefield Validation Not Working

In my Model (Images.php)

public function rules()


		// NOTE: you should only define rules for those attributes that

		// will receive user inputs.

		return array(

			array('images_name', 'required'),

			array('images_name', 'file','types'=>'jpg, gif, png', 'allowEmpty'=>false, 'on'=>'update'),

			// The following rule is used by search().

			// Please remove those attributes that should not be searched.

			array('images_id, fk_deal_id, images_priority, images_name, images_status', 'safe', 'on'=>'search'),



In my Controller

public function actionCreate()


		$model=new Deals;

		$model_details=new DealDetails;

		$model_image = new Images;

		// Uncomment the following line if AJAX validation is needed







			$model_image->attributes = $_POST['Images'];

			$rand_name = rand(100,9999);


			$fileName = "{$rand_name}-{$uploadedFile}";

			$model_image->images_name = $fileName;


			$validate = $model_image->validate();

			$validate = $model_details->validate() && $validate;

			$validate = $model->validate() && $validate;
















In My view

<div class="row">

        <?php echo $form->labelEx($model_image,'images_name'); ?>

        <?php echo $form->fileField($model_image, 'images_name'); ?>  // by this we can upload image

        <?php echo $form->error($model_image,'images_name'); ?>


Ajax Validation is working Fine, But when I submit the form without image the validation not working

Please Help me


I have the same problem, could you solve it? Could you tell me how?


put a condition before this line



$model_image->images_name = $fileName;


Hi try this


array('image_name', 'file', 'allowEmpty' => TRUE,'types' => 'jpg, jpeg, gif, png','on'=>'update'),

when you create the image filed madatory and update the page if image not madatory set the ‘allowEmpty’ => TRUE,

I have done so:

$_POST[‘Book’][‘image’] = $_FILES[‘Book’][‘name’][‘image’];


$model->attributes = $_POST[‘Book’];