Yii2: Default values in ActiveRecord/Model

Created on 13 Mar 2014  路  4Comments  路  Source: yiisoft/yii2

I know there is the default vlaidator which produces a default value but when I create a new model like:

new Series() 

the default rules:

        ['stdo_allowed', 'default', 'value' => 1],
        ['completion_status', 'default', 'value' => 2],

Are not actually applied.

Am I just blind or is there currently no way to set defaults for new objects?

enhancement

Most helpful comment

you can set defaul values by overriding the init() method and set them there.
Default values in validators are to be used when input data is empty.
The difference is as follows:

  • with defaults set in init() the form field gets pre-filled with the value
  • with defaults set in rules this value is used in case the form field is empty.

marked this issue as docs.

All 4 comments

Related to #2726

you can set defaul values by overriding the init() method and set them there.
Default values in validators are to be used when input data is empty.
The difference is as follows:

  • with defaults set in init() the form field gets pre-filled with the value
  • with defaults set in rules this value is used in case the form field is empty.

marked this issue as docs.

Ok kool thanks, my Googling failed me there

Was this page helpful?
0 / 5 - 0 ratings