[size="5"]THE CONTROLLER[/size]
-> function create:
public function actionCreate()
{
$model=new Fotos2;
// Uncomment the following line if AJAX validation is needed
$this->performAjaxValidation($model);
if(isset($_POST['Fotos2']))
{
$model->attributes=$_POST['Fotos2'];
$model->url=CUploadedFile::getInstance($model,'url');
$fecha = date('YmdHms');
$model->url->saveAs(Yii::app()->basePath.'/../images/uploads/'.$fecha.'_'.$model->url);
$model->url = $fecha.'_'.$model->url;
if($model->save())
$this->redirect(array('view','id'=>$model->id));
}
$this->render('create',array(
'model'=>$model,
));
}
->function update:
public function actionUpdate($id)
{
$model=$this->loadModel($id);
// Uncomment the following line if AJAX validation is needed
// $this->performAjaxValidation($model);
if(isset($_POST['Fotos2']))
{
$model->attributes=$_POST['Fotos2'];
$model->url=CUploadedFile::getInstance($model,'url');
$fecha = date('YmdHms');
if ( (is_object($model->url) && get_class($model->url)==='CUploadedFile')) {
$model->url->saveAs(Yii::app()->basePath.'/../images/uploads/'.$fecha.'_'.$model->url);
$model->url = $fecha.'_'.$model->url;
}
if($model->save())
$this->redirect(array('view','id'=>$model->id));
}
$this->render('update',array(
'model'=>$model,
));
}
[size="5"]MODEL[/size]
Declare the public property url (the name of my upload file input)
class Fotos2 extends CActiveRecord
{
public $url;
/**
* Returns the static model of the specified AR class.
* @return Fotos2 the static model class
....
in the public function rules, add this array:
...
array('url', 'unsafe'), //in order to can update the record without upload the file again when is not necessary
array('url', 'file', 'types'=>'jpg, gif, png'), //validate the file extension
[size="5"]THE VIEW _FORM[/size]
<div class="form">
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'fotos2-form',
'enableAjaxValidation'=>true,
'htmlOptions'=>array('enctype'=>'multipart/form-data'),
)); ?>
<p class="note">Fields with <span class="required">*</span> are required.</p>
<?php echo $form->errorSummary($model); ?>
<div class="row">
<?php echo $form->labelEx($model,'nombre'); ?>
<?php echo $form->textField($model,'nombre',array('size'=>50,'maxlength'=>50)); ?>
<?php echo $form->error($model,'nombre'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'url'); ?>
<?php echo $form->fileField($model,'url',array('size'=>60,'maxlength'=>255)); ?>
<?php echo $form->error($model,'url'); ?>
</div>
<div class="row buttons">
<?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save'); ?>
</div>
<?php $this->endWidget(); ?>
</div><!-- form -->
I hope this guide help son want who are getting error like "the filexx cannot be blank"