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.',),
....