I have the following validation rule to set default value for my model:
['from', 'default', 'value' => 2],
It works like a charm when dealing with this model in RESTful application. I.e. if PUT or POST requests are not providing this value, it is automatically set to given default value.
Rules you declare are for validating when model is being loaded with values after the form is submitted. When you prepare data for ‘create’ view you are create new, empty instance. So you have to load attributes value implicitely by calling https://www.yiiframework.com/doc/api/2.0/yii-db-activerecord#loadDefaultValues()-detail (if you did set them in database schema - migrations) or explicitely for example in object constructor or in the controller. Another option is to define scenario in your model applying only ‘default’ rules in it and then calling ‘validate’ method on object.
If I do so, if I separate all the default-like rules to some special scenario, will it be automatically triggered by Yii during create and update? Or by adding scenario to some attribute I am explicitly saying that Yii should not trigger this rule automatically and I must manually trigger it during each $model->save()?