Yii场景 - 验证不会触发

/**
   * @inheritdoc
   */
  public function rules() {
    return [
      [['quantity', 'first_name', 'last_name', 'email', 'country', 'postal_code', 'locality', 'address'], 'required'],
      [['quantity'], 'integer'],
      [['first_name', 'last_name', 'email', 'country', 'phone'], 'string', 'max' => 127],
      [['postal_code'], 'string', 'max' => 20],
      [['locality', 'address'], 'string', 'max' => 255]
    ];
  }

    public function scenarios() {
        return [
            'firstStep' => ['quantity', 'first_name', 'last_name', 'email'],
            'secondStep' => ['country', 'postal_code', 'locality', 'address', 'phone'],
        ];
    }

When I submit the form I get:

Invalid Parameter – yii\base\InvalidParamException

Unknown scenario: default

Does anyone knows why? Perhaps this is not the proper way to overwrite the scenario method.

Do you extends ActiveRecord in your model?

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

 public function rules() {
return [
  [['quantity', 'first_name', 'last_name', 'email', 'country', 'postal_code', 'locality', 'address'], 'required'],
  [['quantity'], 'integer'],
  [['first_name', 'last_name', 'email', 'country', 'phone'], 'string', 'max' => 127],
  [['postal_code'], 'string', 'max' => 20],
  [['locality', 'address'], 'string', 'max' => 255],

  [['quantity', 'first_name', 'last_name', 'email'], 'required', 'on' => 'firstStep'],
  [['country', 'postal_code', 'locality', 'address', 'phone'], 'required', 'on' => 'secondStep'],
];
}

 you change last two line....

 And now use scenario in your controller...

 $model->scenario = 'firstStep';

 or, 

 $model->scenario = 'secondStep';

Ok. Strictly speaking, the reason why I was having this error was due to the fact that I was NOT properly overwriting the scenario method, because, doing like I did, I wouldn't preserve the overwritten method specificities. I would do a full replace on RETURN, given this default error.

In order to avoid this, I have to properly doing it, as it states on the docs:

ie:

public function scenarios() {

            $scenarios = parent::scenarios();

            $scenarios[self::SCENARIO_FIRST_STEP] = ['quantity', 'first_name', 'last_name', 'email'];
            $scenarios[self::SCENARIO_SECOND_STEP] = ['country', 'postal_code', 'locality', 'address', 'phone'];

            return $scenarios;

    }

When we do this, the error is gone.

@Vishu Patel, however, did address the implicit issue I was having. So I will accept it's answer.

Because you are overriding scenarios so you lost the default scenario. you have to do it as follow

public function scenarios() {
    $scenarios = parent::scenarios(); // This will cover you
    $scenarios['firstStep'] = ['quantity', 'first_name', 'last_name', 'email'];
    $scenarios['secondStep'] = ['country', 'postal_code', 'locality', 'address', 'phone'];
    return $scenarios;

}