I
I
Ivan Lykov2018-03-25 19:31:40
Yii
Ivan Lykov, 2018-03-25 19:31:40

How to pass the correct parameters to save data to the database?

Folks, good time. My stupid head does not understand anything, tell me what I'm doing wrong?
Model

<?php

namespace backend\models;

use Yii;

/**
 * This is the model class for table "films".
 *
 * @property int $id
 * @property string $title
 * @property string $desk
 * @property string $text
 * @property string $id_alias
 * @property string $id_category
 * @property string $id_theater
 * @property string $data_out
 * @property string $budget
 * @property string $author
 * @property string $imageFile
 *
 * @property Categoryes $category
 * @property Theaters $theater
 * @property Categoryes $alias
 */
class Films extends \yii\db\ActiveRecord
{

    public $imageFile; 
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'films';
    }

    /**
     * @inheritdoc
     */


    public function rules()
    {
        return [
            [['title', 'desk', 'text', 'id_alias', 'id_category', 'id_theater', 'data_out', 'budget', 'author','imageFile'], 'required'],
            [['text'], 'string'],
            [['data_out'], 'safe'],
            [['imageFile'], 'file', 'skipOnEmpty' => true, 'extensions' => 'png, jpg'],
            [['title', 'desk', 'id_alias', 'id_category', 'id_theater', 'budget', 'author'], 'string', 'max' => 255],
            [['id_category'], 'exist', 'skipOnError' => true, 'targetClass' => Categoryes::className(), 'targetAttribute' => ['id_category' => 'category']],
            [['id_theater'], 'exist', 'skipOnError' => true, 'targetClass' => Theaters::className(), 'targetAttribute' => ['id_theater' => 'theater']],
            [['id_alias'], 'exist', 'skipOnError' => true, 'targetClass' => Categoryes::className(), 'targetAttribute' => ['id_alias' => 'alias']],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'title' => 'Title',
            'desk' => 'Desk',
            'text' => 'Text',
            'id_alias' => 'Id Alias',
            'id_category' => 'Id Category',
            'id_theater' => 'Id Theater',
            'data_out' => 'Data Out',
            'budget' => 'Budget',
            'author' => 'Author',
            'imageFile' => 'Image File',
        ];
    }
    /**
     * @return \yii\db\ActiveQuery
     */
    public function getCategory()
    {
        return $this->hasOne(Categoryes::className(), ['category' => 'id_category']);
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getTheater()
    {
        return $this->hasOne(Theaters::className(), ['theater' => 'id_theater']);
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getAlias()
    {
        return $this->hasOne(Categoryes::className(), ['alias' => 'id_alias']);
    }

    public function upload()
    {
        if($this->validate()){
            $this->imageFile->saveAs("img/{$this->imageFile->baseName}.{$this->imageFile->extension}");
            return true;
        } else {

            return false;
        }
    }
}

Controller method
public function actionCreate()
    {
        $model = new Films();

        if ($model->load(Yii::$app->request->post())) {

            $model->imageFile = UploadedFile::getInstance($model,'imageFile');

            if($model->imageFile && $model->upload()) {
                $model->imageFile = $this->imageFile->baseName . '.' . $this->imageFile->extension;
            }
            if($model->save()) {

                return $this->redirect(['creat', 'id' => $model->id]);

            }
        }

        return $this->render('create', [
            'model' => $model,
        ]);
    }

Form for submitting data to the database
<?php 

    $params = [
       'promt' => 'не указано'
    ]
     ?>

    <?php $form = ActiveForm::begin(['options' => ['enctype' => 'multipart/form-data']]); ?>

        <?= $form->field($model, 'title')->textInput(['maxlength' => true]) ?>

        <?= $form->field($model, 'desk')->textInput(['maxlength' => true]) ?>

        <?= $form->field($model, 'text')->textarea(['rows' => 6]) ?>
        
        <?= $form->field($model, 'id_category')->dropDownList(ArrayHelper::map(Categoryes::find()->all(),'category','category'),$params) ?>

        <?= $form->field($model, 'id_alias')->dropDownList(ArrayHelper::map(Categoryes::find()->all(),'alias','alias'),$params) ?>

        <?= $form->field($model, 'data_out')->textInput() ?>

        <?= $form->field($model, 'budget')->textInput(['maxlength' => true]) ?>

        <?= $form->field($model, 'author')->textInput(['maxlength' => true]) ?>

        <?= $form->field($model, 'id_theater')->dropDownList(ArrayHelper::map(Theaters::find()->all(),'theater','theater'),$params) ?>

        <?= $form->field($model, 'imageFile')->fileInput() ?>

        <div class="form-group">
            <?= Html::submitButton('Save', ['class' => 'btn btn-success']) ?>
        </div>

    <?php ActiveForm::end(); ?>

The bottom line is, when sending, I get an error that I didn’t feed it with one parameter. This parameter is on the form and a picture goes through it, it is also in the model. And he says that he doesn’t ... :(
5ab7ce6295c4e274773960.png
What am I missing, tell me please? Where to specify this most missed parameter?
Thank you in advance

Answer the question

In order to leave comments, you need to log in

[[+comments_count]] answer(s)
D
Dmitry, 2018-03-25
@slo_nik

Good evening.
The error says "does not have a default value".
Remove imageFile from the required section in the validation rules or in the same rules, assign the default value through the default parameter.

Note: Most validators do not process empty inputs if their [[yii\base\Validator::skipOnEmpty]] property defaults to true. They will simply be skipped during validation if their associated attributes are empty. Among the main validators, only captcha, default, filter, required, and trim will handle empty inputs.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question