Yii2 method: scripts ()

There are 2 necessary functions: set a password during registration and change the password if the user has forgotten it. When a user signs up, the password must be at least 4 characters long; when changes take place - at least 5 characters.

Viewing is common for registration and passage changes. Obviously, there are also 2 actions that use either the "register" or "change" scenario. Snippet of code in the model:

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

      

But I want to do it through scripts (). How to do it? I'm new to Yii so I didn't understand when and how to use scripting (). Thank.

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

+3


source to share


1 answer


As the documentationscenarios()

says: The default implementation of this method will return all scripts found in the rules () declaration. So usually you don't need to override this method because it will look for array keys on

to set active attributes for the current script to validate correctly.



So, in your case, 'on' => 'some scenario'

for different checks on the same attribute exactly what you need.

+6


source







All Articles