简体   繁体   中英

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 - Cropper

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. However, it always saves it as 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(); ?>

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

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 :

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

and in validation rules:

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

in addNew() function:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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