Passing Validation Errors

Hi Everybody,

I’m new to yii and I’m not sure what I’m doing wrong.

I’m trying to create a file upload widget but I’m not sure how I’m meant to pass the errors back to the form.

I call my widget using the below code.




$this->widget('ext.gallery.UploadManager', array(

    'id' => $this->id,

    'controllerRoute' => '/admin/gallery',

));



The widget creates a form and on submit gets sent to the Image controller.




public function actionUpload()

	{

        $model = new Image();

        $model->attributes = $_POST['Image'];

        $model->uploaded = date('Y-m-d H:i:s');

        var_dump($model->validate());

        if($model->validate())

        {

            $imageFile = CUploadedFile::getInstanceByName('Image[file]');

            $model->file_name = $imageFile->getName();

            $model->save();

            if($model->save())

            {

                $imageFile->saveAs('gallery/'.$model->file_name);

                // redirect to success page

            }

        }

        else

        {

//            $errors = $model->getErrors();

//            var_dump($errors);

        }

}



But I’m not sure how to get back to my form with the errors.

I’m pretty sure I’m doing things in a roundabout way so any help is appreciated.

Thanks

Are you using an ActiveForm? If so, have a look at

http://www.yiiframework.com/doc/api/1.1/CActiveForm#error-detail

Otherwise, this might help you:

http://www.yiiframework.com/doc/api/1.1/CHtml#errorSummary-detail

why you can not set on model rules function like


array('image_name','required','on'=>'create'),

       

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

Also you can check the hasError


 if (!$model->hasErrors()) {

              //sucess

 }else{

     //set flash messge


}



Thanks for the replys, I had to rewrite a good bit of code to get it working. For some reason I was trying to use 2 controller for 1 action. But all is well now :)