Difference between #39 and #40 of
Yii v2 snippet guide

Revision #40 has been created by rackycz on Sep 20, 2019, 11:32:20 AM with the memo:

db
« previous (#39) next (#41) »

Changes

Title unchanged

Yii v2 for beginners

Category unchanged

Tutorials

Yii version unchanged

2.0

Tags unchanged

tutorial,beginner,yii2

Content changed

[...]
// Why? We dont want to load password-hash from DB and display it to the user
// We only want him to see empty field and if it is filled in, password is changed on background
public $password_new;
public $password_new_repeat;

    // Use this scenario in UserController->actionCreate() right after: $model = new User() like this:
 
    // $model->scenario = User::SCENARIO_CREATE;
 
    // This will force the user to enter the password when new user is created
 
    // When user is edited, new password is not needed
 
    const SCENARIO_CREATE = "user-create";
 
 
// ----- Default 3 model-methods by GII:     
public static function tableName() {
return 'user';
[...]
return [
[['username', 'email'], 'required'],
            [['password_new_repeat', 'password_new'], 'required', "on" => self::SCENARIO_CREATE],
 
[['username', 'email'], 'string', 'max' => 45], ['email', 'email'], [['password', 'authKey'], 'string', 'max' => 60], [['password', 'password_new_repeat', 'password_new'], 'safe'], ['password_new_repeat', 'compare', 'operator' => '==', 'compareAttribute' => 'password_new'], ['password_new_repeat', 'setPasswordWhenChanged'],
];
}
[...]
// ----- Password validator
     public function setPasswordWhenChanged($attribute_name, $params) {          if (trim($this->password_new_repeat) === "") { return true; }          if ($this->password_new_repeat === $this->password_new) { $this->password = sha1($this->password_new_repeat); }          return true; }     
// ----- IdentityInterface methods:

public static function findIdentity($id) {
return static::findOne($id);
}
[...]
7 0
4 followers
Viewed: 275 323 times
Version: 2.0
Category: Tutorials
Written by: rackycz
Last updated by: rackycz
Created on: Sep 19, 2019
Last updated: a year ago
Update Article

Revisions

View all history