【问题标题】:Yii2 - How to get the data from a Widget when submiting in a model?Yii2 - 在模型中提交时如何从小部件中获取数据?
【发布时间】:2017-05-04 18:33:33
【问题描述】:

我正在使用 Widget 裁剪图像并将其上传到 Yii2 的服务器中。
Yii2 - Cropper

我已经根据小部件的文档在视图中实现了小部件,并将数据提交给模型,该模型应该将上传文件的名称保存到数据库中的新条目中。但是,它始终将其保存为 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();
        }
    }
}

当我尝试保存数据时,图像中出现 Null。我不知道如何从 wiget 中获取数据以提取模型中图像的名称。如何访问这些数据?

【问题讨论】:

    标签: php view model yii2 widget


    【解决方案1】:

    添加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...
    ]);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-30
      • 2020-09-30
      • 1970-01-01
      • 1970-01-01
      • 2021-11-02
      相关资源
      最近更新 更多