php - Yii 2.0 : how to set different rules on same attribute for different scenerio -



php - Yii 2.0 : how to set different rules on same attribute for different scenerio -

i need validate file field in yii 2.0. rule file field 'name' looks this.

[['name'], 'file', 'skiponempty'=>false, 'extensions'=>'jpg, jpeg, gif, png', 'maxsize'=>'1024']

i have 2 scenarios insert , update. insert, need 'skiponempty'=>false , update need 'skiponempty'=>true.

how can accomplish in yii 2.0?

just create 2 validation rules required scenarions:

[['name'], 'file', 'skiponempty'=>false, 'extensions'=>'jpg, jpeg, gif, png', 'maxsize'=>'1024', 'on'=>'insert'] [['name'], 'file', 'skiponempty'=>true, 'extensions'=>'jpg, jpeg, gif, png', 'maxsize'=>'1024', 'on'=>'update']

and in controller when initialize model, set required scenario:

$model = new product; $model->scenario = 'update';

official docs: http://www.yiiframework.com/doc-2.0/guide-structure-models.html

php yii2

Comments

Popular posts from this blog

formatting - SAS SQL Datepart function returning odd values -

c++ - Apple Mach-O Linker Error(Duplicate Symbols For Architecture armv7) -

php - Yii 2: Unable to find a class into the extension 'yii2-admin' -