Rafalsky/HomeFinance

View on GitHub
wallet/models/AccountForm.php

Summary

Maintainability
D
1 day
Test Coverage
<?php

/*
 * This file is part of the HomeFinanceV2 project.
 *
 * (c) Rafalsky.com <http://github.com/Rafalsky/>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace wallet\models;

use yii\base\Model;

/**
 * Account form
 */
class AccountForm extends Model
{
    public $username;
    public $email;
    public $password;
    public $password_confirm;

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            ['username', 'filter', 'filter' => 'trim'],
            ['username', 'required'],
            ['username', 'unique',
                'targetClass' =>  '\common\models\User',
                'message' => \Yii::t('backend', 'This username has already been taken.'),
                'filter' => function($query) {
                    $query->andWhere(['not', ['id' => \Yii::$app->user->id]]);
                }
            ],
            ['username', 'string', 'min' => 1, 'max' => 255],
            ['email', 'filter', 'filter' => 'trim'],
            ['email', 'required'],
            ['email', 'email'],
            ['email', 'unique',
                'targetClass' => '\common\models\User',
                'message' => \Yii::t('backend', 'This email has already been taken.'),
                'filter' => function($query) {
                    $query->andWhere(['not', ['id' => \Yii::$app->user->getId()]]);
                }
            ],
            ['password', 'string'],
            [['password_confirm'], 'compare', 'compareAttribute' => 'password']
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'username' => \Yii::t('backend', 'Username'),
            'email' => \Yii::t('backend', 'Email'),
            'password' => \Yii::t('backend', 'Password'),
            'password_confirm' => \Yii::t('backend', 'Password Confirm')
        ];
    }
}