Yii2 rules

I have the following rules:




public function rules()

    {

        return [

            [['title', 'course_topic_id'], 'required', 'on' => 'create'],

            [['title', 'course_topic_id', 'active'], 'required', 'on' => 'update'],

            [['title', 'description'], 'string'],

            [['course_topic_id', 'active'], 'integer'],

            ['active', 'default' => 1],

            [['date_added'], 'safe']

        ];

    }



When someone creates a new question record I am trying to set the ‘active’ field using the default rule. But I get Invalid validation rule: a rule must specify both attribute names and validator type.

Well it’s exactly what the error message says. You did not specify the attribute name and the validator type for ‘active’ field.




public function rules()

    {

        return [

            [['title', 'course_topic_id'], 'required', 'on' => 'create'],

            [['title', 'course_topic_id', 'active'], 'required', 'on' => 'update'],

            [['title', 'description'], 'string'],

            [['course_topic_id', 'active'], 'integer'],

            [['active'], 'default', 'value' => 1],

            [['date_added'], 'safe']

        ];

    }



Please note, I haven’t tested this but it looks fine.

Also you might want to check this tutorial-core-validators.md and more specificaly this tutorial-core-validators.md#default

Thanks Alain