public function validate()
{
if (empty($this->name)) {
throw new Exception(__('Name required.'));
}
if (empty($this->password)) {
throw new Exception(__('Password required.'));
}
if (!preg_match('/^[a-zA-Z0-9._\\-]{3,}$/', $this->username)) {
throw new Exception(__('Username is invalid.'));
}
// TODO: email validation differs from email validation in vuejs
if (!filter_var($this->email, FILTER_VALIDATE_EMAIL)) {
throw new Exception(__('Email is invalid.'));
}
if (self::where(['id <> :id'], ['id' => $this->id ?: 0])->where(function ($query) {
$query->orWhere(['LOWER(username) = :username', 'LOWER(email) = :username'], ['username' => strtolower($this->username)]);
})->first()) {
throw new Exception(__('Username not available.'));
}
if (self::where(['id <> :id'], ['id' => $this->id ?: 0])->where(function ($query) {
$query->orWhere(['LOWER(username) = :email', 'LOWER(email) = :email'], ['email' => strtolower($this->email)]);
})->first()) {
throw new Exception(__('Email not available.'));
}
return true;
}