Hi, I don’t know if this is a bug on Yii, or just a bug in my code.
But everytime I upload any file, even the one that satisfies the rules defined in my Model,
The ajax validation is insisting that the file is blank.
918
Here is the code in my view file send.php
<div class="form">
<?php $form=$this->beginWidget('CActiveForm',array(
'id'=>'send-form',
'enableAjaxValidation'=>true,
'htmlOptions'=>array('enctype'=>'multipart/form-data'),
));
?>
<?php echo $form->errorSummary($model); ?>
<div class="row">
<?php echo $form->error($model,'picture'); ?>
<?php echo $form->label($model,'picture'); ?>
<?php echo $form->fileField($model,'picture'); ?>
</div>
<div class="row submit">
<?php echo CHtml::submitButton('Submit'); ?>
</div>
<?php $this->endWidget(); ?>
</div><!-- form -->
Here is the code in my model file PictureSendForm.php
<?php
class PictureSendForm extends CFormModel
{
public $picture;
//Declares the validation rules.
public function rules()
{
return array(
array('picture','safe'),
array('picture','file','types'=>'jpg','allowEmpty'=>false),
);
}
public function attributeLabels()
{
return array(
'picture'=>'Picture',
);
}
public function process()
{
$file = 'files/pics/original/t'.Yii::app()->user->id.'.jpg';
$w_max = 600;
list($w, $h) = getimagesize($file);
$ratio = $w/$h;
$h_max = $w_max/$ratio;
$image_input = imagecreatefromjpeg($file);
$image_output = imagecreatetruecolor($w_max,$h_max);
imagecopyresampled(
$image_output,
$image_input,
0,0,0,0,
$w_max,$h_max,
$w,$h);
imagejpeg($image_output,$file,100);
}
}
?>
And the code in my controller file DefaultController.php
public function actionSend()
{
$this->layout = '//layouts/column1';
$model = new PictureSendForm;
$this->performAjaxValidation($model);
if(isset($_POST['PictureSendForm']))
{
//$model->attributes = $_POST['PictureSendForm'];
$model->picture = CUploadedFile::getInstance($model,'picture');
if($model->validate()){
$model->picture->saveAs('files/pics/original/t'.Yii::app()->user->id.'.jpg');
$model->process(); //Resizes the image
$this->redirect('edit');
}
}
$this->render('send', array('model'=>$model));
}
protected function performAjaxValidation($model)
{
if(isset($_POST['ajax']))
{
if($_POST['ajax']==='send-form'){
echo CActiveForm::validate($model);
Yii::app()->end();
}
}
}
But if I clicked the Submit button, it is able to redirect to the Edit action.
Hope someone can help me, thank you in advance.