I’ve created a file upload form and i’ve written the validation rules to handle the image, when I select the image and save the image in a correct way all is ok, but when i choose upload the image in a wrong way (eg exceeding the maximum allowed size image) my web application dose not show the error message in the corresponding view, however the rule complies with the validation of the maximum of the image and not allow the image loading.
The issue is that i have to show the error message, how can i do it?
View:
<?php echo CHtml::form('','post',array('enctype'=>'multipart/form-data')); ?>
<?php
echo CHtml::activeFileField($model, 'imagenurl'); ?>
<div class="row buttons">
<?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save'); ?>
</div>
<?php echo CHtml::endForm(); ?>
Controller:
public function actionUpdateimg()
{
$model=$this->loadModel();
$theTime = date("dmy");
// Uncomment the following line if AJAX validation is needed
// $this->performAjaxValidation($model);
if(isset($_POST['Producto']))
{
$model->attributes=$_POST['Producto'];
//$model->imagenurl=CUploadedFile::getInstance($model,'imagenurl');
$myfile=CUploadedFile::getInstance($model,'imagenurl');
if (is_object($myfile) && get_class($myfile)==='CUploadedFile')
$model->imagenurl=$model->idProducto.'_'.$theTime.'_'.$myfile->name;
if($model->save())
// $model->imagenurl->saveAs('../images/'.$model->idProducto.'_'. $model->imagenurl);
if (is_object($myfile))
$myfile->saveAs('../images/'.$model->imagenurl);
$this->redirect(array('view','id'=>$model->idProducto));
}
$this->render('updateimg',array(
'model'=>$model,
));
}
Model:
public $imagenurl;
// $imagenurl= $imagenurl;
public function rules()
{
// NOTE: you should only define rules for those attributes that
// will receive user inputs.
return array(
array('Descripcion', 'length', 'max'=>200),
array('Marca, Clase', 'length', 'max'=>100),
array('Tamano, Carac', 'length', 'max'=>45),
array('Carac', 'required'),/* valida campo requerido*/
array('imagenurl', 'file', 'allowEmpty'=>true, 'types'=>'jpg,gif', 'maxSize'=>409600,'tooLarge'=>'The file was larger than 400K. Please upload a smaller file.'),
array('idProducto, Descripcion, Marca, Clase, Tamano', 'safe', 'on'=>'search'),
);
}