簡體   English   中英

Yii2-在模型中提交時如何從小部件獲取數據?

[英]Yii2 - How to get the data from a Widget when submiting in a model?

我使用小部件將圖片裁剪並通過Yii2將圖像上傳到服務器。
Yii2-作物

我已經基於Widget的文檔在視圖中實現了該Widget,並將數據提交到一個模型,該模型應該將上傳文件的名稱保存到數據庫的新條目中。 但是,它始終將其保存為Null。

newform.php

<?php
    use yii\helpers\Html;
    use yii\widgets\ActiveForm;
    use common\budyaga\cropper\Widget;
    use yii\helpers\Url;
    $this->title='Upload New';
?>
<h1><?= Html::encode($this->title) ?></h1>
<?php $form = ActiveForm::begin(); ?>
<?= $form->field($model, 'title') -> textInput();?>
<?= $form->field($model, 'description') -> textInput(); ?>
<?= $form->field($model, 'link') -> textInput(); ?>
<?= $form->field($model, 'image')-> widget(Widget::className(), [
    'uploadUrl' => Url::toRoute('/admin/uploadPhoto'),
    'width' => 236,
    'height' => 234,
    'cropAreaWidth' => 500,
    'cropAreaHeight' => 500,
    'thumbnailWidth' => 236,
    'thumbnailHeight' => 234,
    ]);?>
<div class="form-group">
    <?= Html::submitButton('Save', ['class' => 'btn btn-primary']); ?>
</div>
<?php ActiveForm::end(); ?>

控制器中的方法

public function actionNews(){
    $model = new NewsForm();
    $model->setScenario('addNew');
    if ($model->load(Yii::$app->request->post()) && $model->addNew()) {
        return $this->render('newsmanager');
    } else {
        return $this->render('newform', ['model' => $model]);
    }
}

模型

class Newnews extends \yii\db\ActiveRecord{
    public static function tableName()
    {
        return 'news';
    }

    public function rules()
    {
       return [
            [['visits', 'user_id'], 'integer'],
            [['user_id'], 'required'],
            [['title', 'image'], 'string', 'max' => 45],
            [['description', 'link'], 'string', 'max' => 255],
            [['user_id'], 'exist', 'skipOnError' => true, 'targetClass' => User::className(), 'targetAttribute' => ['user_id' => 'id']],
       ];
    }

    public function attributeLabels()
    {
        return [
            'idnew' => 'Idnew',
            'title' => 'Title',
            'description' => 'Description',
            'image' => 'Image',
            'visits' => 'Visits',
            'user_id' => 'User_ID',
            'link' => 'Link',
            'dateu' => 'Dateu'
        ];
    }
}

NewsForm.php

class NewsForm extends Model
{
    public $image;
    public $title;
    public $description;
    public $link;

    public function rules()
    {
        return [
            [['title','description','link'],'required'],
            ['image','safe', 'on'=>'addNew']
        ];
    }

    public function addNew(){
        if($this->validate()){
            $newnew=new Newnew();
            $newnew->title=$this->title;
            $newnew->description=$this->description;
            $newnew->link=$this->link;
            $newnew->image;
            $newnew->user_id=Yii::$app->getUser()->getId();
            $newnew->visits=0;
            $newnew->dateu=date("Y-m-d H:i:s");
            return $newnew->save();
        }
    }
}

當我嘗試保存數據時,圖像中顯示為空。 我不知道如何從假發中獲取數據以提取模型中圖像的名稱。 如何訪問此數據?

NewsForm中添加:

 /**
 * @var UploadedFile
 */
public $imageFile;

並在驗證規則中:

[['imageFile'], 'file', 'mimeTypes' => 'image/jpeg, image/png', 'maxSize' => 5000000],

addNew()函數中:

$this->imageFile = UploadedFile::getInstance($this, 'imageFile');
$file = $this->imageFile;
$fname = $file->baseName
$fext = $file->extension
//etc...
//then validate and save file and model as you want.

以視圖形式:

<?= $form->field($model, 'imageFile ')-> widget(Widget::className(), [
//config...
]);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM