2016-03-20 04:55:26 +05:30
|
|
|
<?php
|
|
|
|
namespace api\models;
|
|
|
|
|
2016-03-20 05:03:49 +05:30
|
|
|
use api\models\base\PasswordProtectedForm;
|
2016-03-20 04:55:26 +05:30
|
|
|
use common\models\Account;
|
|
|
|
use Yii;
|
|
|
|
use yii\helpers\ArrayHelper;
|
|
|
|
|
2016-03-20 05:03:49 +05:30
|
|
|
class ChangeUsernameForm extends PasswordProtectedForm {
|
2016-03-20 04:55:26 +05:30
|
|
|
|
|
|
|
public $username;
|
|
|
|
|
|
|
|
public function rules() {
|
|
|
|
return ArrayHelper::merge(parent::rules(), [
|
|
|
|
[['username'], 'required', 'message' => 'error.{attribute}_required'],
|
|
|
|
[['username'], 'validateUsername'],
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function validateUsername($attribute) {
|
|
|
|
$account = new Account();
|
|
|
|
$account->username = $this->$attribute;
|
|
|
|
if (!$account->validate(['username'])) {
|
|
|
|
$account->addErrors($account->getErrors('username'));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function change() {
|
|
|
|
if (!$this->validate()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$account = $this->getAccount();
|
|
|
|
$account->username = $this->username;
|
|
|
|
|
|
|
|
return $account->save();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|