"default" scenario impacts on new scenarios in validation.

Hi, I need help.

This is validation rules of SignupForm. I’m added rule with “first_admin” scenario. In this scenario I’m need check ONLY exist fields username and password, BUT Yii2 doing processing “default” scenario.

On validation i get error about length of password, but my "first_admin" scenario MUST pass this check.

    public function rules()


        return [

            ['username', 'trim'],

            ['username', 'required'],

            ['username', 'unique', 'targetClass' => '\common\models\User', 'message' => 'This username has already been taken.'],

            ['username', 'string', 'min' => 2, 'max' => 255],

            ['email', 'trim'],

            ['email', 'required'],

            ['email', 'email'],

            ['email', 'string', 'max' => 255],

            ['email', 'unique', 'targetClass' => '\common\models\User', 'message' => 'This email address has already been taken.'],

            ['password', 'required'],

            ['password', 'string', 'min' => 6],

            [['username', 'password'], 'required', 'on' => 'first_admin'],



My model call:

    public function createFirstAdmin() {

        // -- add first admin user

        $model = new SignupForm(['scenario' => 'first_admin']);

        $model->username = 'admin';

        $model->password = 'admin';

        return ( $first_admin = $model->signup() ) ?  $first_admin : null;


P.S. I try declare scenarios() func., but not working.

What the problem?


Rules that have no scenario assigned will always be applied.

If you don’t want a rule applied in a specific scenario, this is your friend: