akshaypjoshi
akshaypjoshi

Reputation: 1295

How can I make all model attributes as safe in Yii2?

Here, I am making all database fields as safe for massive assignment?

[$this->attributes, 'safe', 'on' => self::SCENARIO_MIGRATION],

Is it correct and if not what is the best way to achieve this?

Upvotes: 1

Views: 1226

Answers (1)

rob006
rob006

Reputation: 22174

Validation with safe rule for all attributes does not validate anything, so it does not make much sense. Instead of creating such rules it may be better to skip validation directly in code:

$model->setAttributes($data, false);
$model->save(false);

But if you really need such "empty" validation rules, you should use attributes() method to get list of attributes names:

[$this->attributes(), 'safe', 'on' => self::SCENARIO_MIGRATION],

Upvotes: 4

Related Questions