How can I use rules (FormModel) just if the user choose a file
edit: I mean in controller, how can I check if use choose a file in controller
How can I use rules (FormModel) just if the user choose a file
edit: I mean in controller, how can I check if use choose a file in controller
In FormModel
public function rules()
{
return array(
array('file_id', 'file', 'types'=>'pdf,jpg, gif, png'),
);
}
In controller
$model->attributes=$_POST[‘Item’];
$model->image=CUploadedFile::getInstance($model,'file_id');
if($model->validate())
{
//...the uploaded file is validated, take your desicion <img src='http://www.yiiframework.com/forum/public/style_emoticons/default/smile.gif' class='bbc_emoticon' alt=':)' />
}
if you want to do that automatically just add
array(‘file_id’, ‘file’,‘allowEmpty’=>true, ‘types’=>‘pdf,jpg, gif, png’)
if I don’t choose image it show “Trying to get property of non-object”
ok, after of
$model->image=CUploadedFile::getInstance($model,‘file_id’);
you can check if the file is uploaded
if ($model->image === null) {
//no file submitted
}
OR by pured php code
if (!isset($_FILES[‘file_id’])) {
//no file submitted
}
Ok thanks
I’ve done this
[PHP]
$catgory = Catgory::model()->findByPk($id);
$model = new AdminEditCatgory();
if(isset($_POST[‘AdminEditCatgory’]))
{
$model->attributes = $_POST['AdminEditCatgory'];
$model->image = CUploadedFile::getInstance($model, 'image');
if($model->validate())
{
$catgory->name = $model->name;
if ($model->image === null) {
$catgory->image_type = Helper::getType($model->image->name);
$model->image->saveAs(
DIR.'/static/images/catgory/'.$catgory->id.'.'.Helper::getType($model->image->name)
);
}
$catgory->save();
}
}
[/PHP]
but nothing hasn’t change… the same problem “Trying to get property of non-object” when I don’t choose a picture but if I choose then their is no problem.
Thanks for help
$model->image !== null instead of $model->image === null
Thank you very much