3

There are 2 needed functions: set password when registering and change password, if user forgot it. When user signs up, password length must be at least 4 chars; when changes pass - at least 5 chars.

View is common for registration and changing pass. Obviously, also 2 actions exist, in which either scenario 'signup', either 'change' used. Code snippet in model:

public function rules() {
     return [
       ['password', 'string', 'min' => 4, 'on' => 'signup'],
       ['password', 'string', 'min' => 5, 'on' => 'change'],
     ];
}

But I want to do do it via scenarios(). How to do it? I'm a beginner in Yii, so did not understand, when and how to use scenarios(). Thanks.

UPD. I need to use scenarios() for ONE field with ONE rule, but DIFFERENT arguments to this one rule. how to define a scenario in Yii2? - it is NOT my case.

Community
  • 1
  • 1
Boolean_Type
  • 1,146
  • 3
  • 13
  • 40
  • 1
    possible duplicate of [how to define a scenario in Yii2?](http://stackoverflow.com/questions/31112079/how-to-define-a-scenario-in-yii2) – Tony Jul 06 '15 at 18:22
  • @Tony, no and no! I understand, how to use _scenarios()_ for _required_ validation. But in my situation pass is reqiured in both cases. I need to use scenarios() for ONE field with ONE rule, but DIFFERENT arguments to this one rule. – Boolean_Type Jul 06 '15 at 18:33
  • @Tony, sorry, it seems, I deleted your comment accidentally. I tried, but behaviour of code becomes incorrect. I just need example, u see? If I write, as u said: _$scenarios['signup'] = ['password'];_ and _$scenarios['signin'] = ['password'];_, how my _rules()_ method need to be changed? As I understand, I have to delete _on_ when using _scenarios()_. But it will not be correct. Otherwise, if I don’t need to change _rules()_, what sense to write _scenarios()_? It works great without it. – Boolean_Type Jul 06 '15 at 19:17

1 Answers1

6

As documentation about scenarios() says: The default implementation of this method will return all scenarios found in the rules() declaration. So generally you do not need to override this method, because it will look for on array keys to set active attributes for current scenario an validate them properly.

So in your case 'on' => 'some scenario' for different validations of the same attribute is exactly what you need.

Tony
  • 5,797
  • 3
  • 26
  • 22
  • 2
    thanks a lot! I think, I begin to understand diff between _on_ and _scenario()_ using. The 1st used, if we need to apply the scenario for concrete **rule**: `['password', 'required', 'on' => 'register'] ['password', 'integer', 'on' => 'another_scenario'] ` And _scenario()_ used, if we need to apply the scenario for concrete **attribute**: `public function scenarios() { $scenarios = parent::scenarios(); $scenarios['signin'] = 'password'; //ALL rules will apply to pass field, when using ‘signin’ scenario return $scenarios; }` Am I right in my assumptions? – Boolean_Type Jul 06 '15 at 19:40
  • 2
    @Boolean_Type: absolutely, that's correct (though you'd do `$scenarios['signin'] = ['password'];`, i.e. use an array of attributes which are set and validated in that scenario). – tarleb Jul 06 '15 at 20:09