Changes
Title
unchanged
Optimize Scenarios for yii2
Category
unchanged
Tutorials
Yii version
unchanged
2.0
Tags
unchanged
model validation,model,validation rules
Content
changed
[...]
```php
class MyModel extends \yii\db\ActiveRecord
{
const SCENARIOCRIEATE = 'scenariocr
ieate';
const SCENARIOUPDATE = 'scenarioupdate';[...]
return [
self::SCENARIOCRIEATE => ['user_id', 'name', 'desc', 'published','date_create'],
self::SCENARIOUPDATE => ['user_id', 'name', 'desc', 'date_update'],
];[...]
$allscenarios = $this->getCustomScenarios();
// published not required
$allscenarios[self::SCENARIOCRIEATE] = array_diff($allscenarios[self::SCENARIOCR
IEATE], ['published']);
return $allscenarios;[...]
$allscenarios = $this->ModifyRequired();
return [
[$allscenarios[self::SCENARIOCRIEATE], 'required', 'on' => self::SCENARIOCR
IEATE],
[$allscenarios[self::SCENARIOUPDATE], 'required', 'on' => self::SCENARIOUPDATE],
[['user_id'], 'integer'],[...]
$model = new MyModel;
$model->scenario = 'scenariocrieate';
if ($model->load(\Yii::$app->request->post())){
// force my columns
if($model->save()){[...]