简体   繁体   English

Yii2-在模型中提交时如何从小部件获取数据?

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

Im using a Widget to crop and upload images into a server with Yii2. 我使用小部件将图片裁剪并通过Yii2将图像上传到服务器。
Yii2 - Cropper Yii2-作物

I've implemented the widget in a view, based on the documentation of the Widget, and submited the data to a model wich should save the name of the uploaded file into a new entry in the database. 我已经基于Widget的文档在视图中实现了该Widget,并将数据提交到一个模型,该模型应该将上传文件的名称保存到数据库的新条目中。 However, it always saves it as Null. 但是,它始终将其保存为Null。

newform.php 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(); ?>

Method in controller 控制器中的方法

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]);
    }
}

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 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();
        }
    }
}

When I try to save the data, I get a Null in image. 当我尝试保存数据时,图像中显示为空。 I dont know how to get the data from the wiget to extract the name of the image in the Model. 我不知道如何从假发中获取数据以提取模型中图像的名称。 How do I acces this data? 如何访问此数据?

add in NewsForm : NewsForm中添加:

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

and in validation rules: 并在验证规则中:

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

in addNew() function: 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.

in view form: 以视图形式:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM