<?php
namespace myresto\models;

use yii\base\Model;
use common\models\User;

/**
 * Signup form
 */
class SetPasswordForm extends Model {
    public $username;
    public $password;

    public function rules() {
        return [
            ['username', 'trim'],
            ['username', 'required'],
            ['username', 'string', 'min' => 2, 'max' => 255],

            ['password', 'required'],
            ['password', 'string', 'min' => 6],
        ];
    }

    public function setPassword() {
        if ($this->validate()) {
            $user = User::findByUsername($this->username);
            if ($user) {
                $user->setPassword($this->password);
                if ($user->save())
                    return true;
                else
                    return false;
            } else {
                $this->addError('username', 'Username tidak ditemukan.');
                return false;
            }
        }
    }
}
