C
C
Chivirdos2017-04-19 15:27:14
Yii
Chivirdos, 2017-04-19 15:27:14

How to set yii 2 safe attributes?

I'm working with the basic version. I collect data from the form and want to write them to the database in bulk, through attributes. Here are my validation rules:

public function rules()
{
    return [
        [['name', 'date', 'parking_place', 'parking_time','spent_fuel', 'remain_fuel', 'distance'],
            'required', 'message' => 'Поле не может быть пустым'],
        [['parking_time','spent_fuel', 'remain_fuel', 'distance'],
            'integer', 'message' => 'Значение должно быть целым числом'],
        [['name', 'date', 'parking_place', 'parking_time','spent_fuel', 'remain_fuel', 'distance'],
            'safe'],
    ];
}

Here's how I'm trying to write data to the database:
if ($model->load(Yii::$app->request->post()) && $model->validate()) {
            $fleet = new Fleet();

            $fleet->setAttributes($model->attributes);

            $fleet->save();
            Yii::$app->session->setFlash('reportFormSubmitted');
            return $this->refresh();
        }

Answer the question

In order to leave comments, you need to log in

[[+comments_count]] answer(s)
B
Boris Yakushev, 2017-04-19
@Chivirdos

those. do you have two models in action? What is it for?

$model = new Fleet()
if (load, validate) {
    $model->save();
    // прочий код
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question