CActiveForm Yii空白数据

时间:2013-11-06 16:11:32

标签: php yii

我正在上传图片并有一个空白的帖子数据。请帮帮我!

    public function actionUpdate()
        {
            if(isset($_GET['TemplateId']))
            {
                $TemplateId = $_GET['TemplateId']; 
            }else{
                $this->setFlash('error', 'Undefined template!');
                $this->render('update');die();
            }
            $model = Store::model()->findByPk($this->module->getStoreId());
            //exit($model->image_logo2);
            //$model = AccountTemplate::model()->findByAttributes(array('account' => $account, 'template' => $TemplateId));
            if($TemplateId==1)
            {
                $last_image = $model->image_logo1;
            }else{
                $last_image = $model->image_logo2;
            }
            if(isset($_POST['Store']))
            {
                    echo "<pre>";print_r($_POST['Store']);die();
                    if($TemplateId==1)
                    {
                        $uploader = CUploadedFile::getInstance($model,'image_logo1');
                    }else{
                        $uploader = \CUploadedFile::getInstance($model,'image_logo2');
                    }
                    $sourcePath = pathinfo($uploader->getName());

                    $fileName = uniqid().'.'.$sourcePath['extension']; 

                    if($sourcePath['extension']!="jpg" && $sourcePath['extension']!="png")
                    {
                        $this->setFlash('error', 'Only jpg or png files!');
                    }else{
                        if($TemplateId==1)
                        {
                            $model->image_logo1 = $fileName; 
                            echo "<pre>";print_r($model->image_logo1);die();
                        }else{
                            $model->image_logo2 = $fileName; 
                            echo "<pre>";print_r($model->image_logo2);die();
                        }
                        if($model->save()) {
                            $uploader->saveAs('uploads/'.$fileName);
                            if(file_exists('uploads/'.$last_image))
                            {
                                //unlink('uploads/'.$last_image);
                            }
                            $this->setFlash('success', 'New logo upload success');
                        }else{
                            $this->setFlash('error', 'New logo not upload!');
                        }
                    }
            }
            $this->render('update', array('model'=>$model, 'TemplateId' => $TemplateId));
        }



    <?php
if(!isset($model))
{

}else{
$form = $this->beginWidget(
    'CActiveForm',
    array(
        'id' => 'upload-form',
        'enableAjaxValidation' => false,
        'htmlOptions' => array('enctype' => 'multipart/form-data'),
    )
);
// ...<img src='/uploads/products/10e83ff7ae580b7e198a38cf9133ff32.jpg'>
// ...
// ...
if($TemplateId==1)
{
    echo $form->labelEx($model, "Logo image");
    echo $form->fileField($model, 'image_logo1');
    echo $form->error($model, 'image_logo1');
}else{
    echo $form->labelEx($model, "Logo image");
    echo $form->fileField($model, 'image_logo2');
    echo $form->error($model, 'image_logo2');
}
echo CHtml::button('Update', array('submit' => array("Template/update/?TemplateId=$TemplateId"))); 
// ...
$this->endWidget();
}
?>

我的观点

    <?php
if(!isset($model))
{

}else{
$form = $this->beginWidget(
    'CActiveForm',
    array(
        'id' => 'upload-form',
        'enableAjaxValidation' => false,
        'htmlOptions' => array('enctype' => 'multipart/form-data'),
    )
);
// ...<img src='/uploads/products/10e83ff7ae580b7e198a38cf9133ff32.jpg'>
// ...
// ...
if($TemplateId==1)
{
    echo $form->labelEx($model, "Logo image");
    echo $form->fileField($model, 'image_logo1');
    echo $form->error($model, 'image_logo1');
}else{
    echo $form->labelEx($model, "Logo image");
    echo $form->fileField($model, 'image_logo2');
    echo $form->error($model, 'image_logo2');
}
echo CHtml::button('Update', array('submit' => array("Template/update/?TemplateId=$TemplateId"))); 
// ...
$this->endWidget();
}
?>

POST输出

 Array
(
    [image_logo1] => 
)

1 个答案:

答案 0 :(得分:0)

谢谢大家的帮助,伙计们!这个错误发生了,因为我不是在我的模型商店中写出attributeLabels

public function attributeLabels()
    {
        return array(
            .....
            'image_logo' => 'Icon',
            'image_logo1' => 'Icon',
            'image_logo2' => 'Icon',
        );
    }