file upload probelm

I have a form submiting with ajax that has the error File cannot be blank why?

controller


public function actionNewphoto() {

      

        $model = new CmPhotos ;

         if(isset($_POST['ajax']) && $_POST['ajax']==='newphotoform')

    { $model->ph_file=CUploadedFile::getInstance($model,'ph_file');

        echo CActiveForm::validate($model);

        Yii::app()->end();

    }

print_r($_POST);

        if (isset($_POST['CmPhotos'])) {

            // collects user input data

         

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

                $model->ph_file=CUploadedFile::getInstance($model,'ph_file');

            $model->pr_id = Yii::app()->user->id; 

           


            // validates user input and redirect to previous page if validated

            if ($model->validate($model)) {

              //  print_r($_POST);

                if ($model->save()) {

                   echo "Your request is done!";

                } else {

                    echo "There is an error,please try again!";

                }

            }

            else

                echo "There is an error,please try again!";

        }

 

    }

view


<div class="form">


    <?php

    $form = $this->beginWidget('CActiveForm', array(

        'id' => 'newphotoform',

        'action' => array('control/newphoto'),

        'htmlOptions'=>array('enctype'=>"multipart/form-data"),

      'enableAjaxValidation' => true,

        'enableClientValidation'=>true,//'enableAjaxValidation'=>false,

        'clientOptions' => array('validateOnSubmit' => true, 'validateOnType' => false)       ));

    ?>


    <p class="note">Fields with <span class="required">*</span> are required.</p>


    <?php echo $form->errorSummary($photos); ?>


    <div class="row">

        <?php echo $form->labelEx($photos, 'ph_title'); ?>

        <?php echo $form->textField($photos, 'ph_title'); ?>

        <?php echo $form->error($photos, 'ph_title'); ?>

    </div>

      <div class="row">

        <?php echo $form->labelEx($photos, 'ph_note'); ?>

        <?php echo $form->textField($photos, 'ph_note'); ?>

        <?php echo $form->error($photos, 'ph_note'); ?>

    </div>

    <div class="row">

        <?php  echo $form->labelEx($photos, 'ph_file'); ?>

        <?php   echo $form->fileField($photos, 'ph_file'); ?>

        <?php  echo $form->error($photos, 'ph_file'); ?>

    </div>





    <div class="row buttons">

        <?php echo CHtml::SubmitButton('Submit'); ?>

    </div>


    <?php $this->endWidget('CActiveForm'); ?>


</div><!-- form -->


<?php $this->endWidget('zii.widgets.jui.CJuiDialog'); 

$this->beginWidget('ext.ajaxform.JAjaxForm',array(

    'formId'=>'newphotoform',

    'options'=>array(

        'dataType'=>'json',

        'beforeSubmit'=>'js:function(formData,$form,options) {alert("ok")   }',

        'success'=>'function(responseText,statusText) { alert(responseText); }',

    ),

));

model


public function rules()

	{

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

		// will receive user inputs.

		return array(

			array('ph_title,  ph_note','required'),

			//array(' ph_file', 'safe'),

                   array('ph_file', 'file', 'types'=>'jpg, gif, png',

                  

                'maxSize'=>1024 * 1024 * 10, // 50MB

                'tooLarge'=>'The file was larger than 50MB. Please upload a smaller file.',),

....

I set ‘allowEmpty’=>true ,then I can not read the post.

Also I want ‘allowEmpty’=>false but then I have the error even I select an image

Because uploaded file information is stored in superglobal $_FILES and not $_POST.

Read this topic http://www.yiiframework.com/forum/index.php?/topic/24334-upload-files-using-tabular-form/page__p__118003__fromsearch__1#entry118003 for detailed explanation.

One question:




...

// validates user input and redirect to previous page if validated

if ($model->validate($model)) {

...



Why you pass model object to validate method? Does you intended like that or is it accidentally?

CModel.validate() accepts array as a first parameter, but if you left blank that parameter it assumes all model attributes from rules array to validate. It will call CValidator.validate() passing it your model object and attributes. And when CValidator.validate() takes in place, it will check if attributes is an array, and if not it will use passed model object attributes.

So resume is, don’t pass model object to validate() method :)

Thanks, I will look for this.

Now my problem is that the ajax submition does not work, the image is uploaded with plain post,also the model is saved.

Ajax does not work,even the alert.




$this->beginWidget('ext.ajaxform.JAjaxForm',array(

    'formId'=>'newphotoform',

    'options'=>array(

        'dataType'=>'json',

        'clearForm'=>true,

        'beforeSubmit'=>'function(formData,$form,options) {return true;   }',

        'success'=>'function(responseText,statusText) {alert("ok")  }',

    ),

)); 

$this->endWidget('ext.ajaxform.JAjaxForm'); 

I use the jquery code and it works.

I want to rename the name of the file that I upload.I use CUploadedFile.

How can I do it?

it has saveAs method i think