add validation to reg

This commit is contained in:
unknown 2025-09-06 05:49:35 +03:00
parent 38a7d08d57
commit baf86cb99b
2 changed files with 158 additions and 42 deletions

View File

@ -3,7 +3,39 @@ from PySide6.QtWidgets import (
QHBoxLayout, QSpacerItem, QSizePolicy QHBoxLayout, QSpacerItem, QSizePolicy
) )
from PySide6.QtCore import Qt from PySide6.QtCore import Qt
from common_lib.utils.validators import validate_username, validate_password from app.ui.widgets.validation_input import ValidationInput
from common_lib.utils.validators import (
validate_username as common_validate_username,
validate_password as common_validate_password
)
def validate_username(username, is_login=False):
if is_login:
if len(username) < 3 or len(username) > 32:
msg = "Неверный логин или пароль"
return False, msg
return True, username
return common_validate_username(username, need_back=True)
def validate_invite_code(invite_code):
return common_validate_username(invite_code, need_back=True)
def validate_password(password, is_login=False):
if is_login:
if len(password) < 3 or len(password) > 32:
msg = "Неверный логин или пароль"
return False, msg
return True, password
return common_validate_password(password, need_back=True)
def validate_name(name):
# Optional field
if not name:
return True, ""
if len(name) >= 32:
return False, "Имя не должно превышать 32 символов"
return True, ""
class LoginView(QWidget): class LoginView(QWidget):
def __init__(self, on_login): def __init__(self, on_login):
@ -70,27 +102,20 @@ class LoginView(QWidget):
self.register_switch.clicked.connect(self.show_register_form) self.register_switch.clicked.connect(self.show_register_form)
def init_register_form(self): def init_register_form(self):
self.name_input = QLineEdit() self.name_input = ValidationInput("Имя")
self.name_input.setPlaceholderText("Имя") self.name_input.set_validator(validate_name, is_required=False)
self.name_input.setFixedHeight(40)
self.reg_login_input = QLineEdit() self.reg_login_input = ValidationInput("Логин")
self.reg_login_input.setPlaceholderText("Логин") self.reg_login_input.set_validator(validate_username)
self.reg_login_input.setFixedHeight(40)
self.reg_password_input = QLineEdit() self.reg_password_input = ValidationInput("Пароль", is_password=True)
self.reg_password_input.setPlaceholderText("Пароль") self.reg_password_input.set_validator(validate_password)
self.reg_password_input.setEchoMode(QLineEdit.Password)
self.reg_password_input.setFixedHeight(40)
self.confirm_password_input = QLineEdit() self.confirm_password_input = ValidationInput("Повторите пароль", is_password=True)
self.confirm_password_input.setPlaceholderText("Повторите пароль") self.confirm_password_input.set_validator(self.validate_confirm_password)
self.confirm_password_input.setEchoMode(QLineEdit.Password)
self.confirm_password_input.setFixedHeight(40)
self.invite_code_input = QLineEdit() self.invite_code_input = ValidationInput("Инвайт-код")
self.invite_code_input.setPlaceholderText("Инвайт-код") self.invite_code_input.set_validator(validate_invite_code, is_required=False)
self.invite_code_input.setFixedHeight(40)
self.register_button = QPushButton("Зарегистрироваться") self.register_button = QPushButton("Зарегистрироваться")
self.register_button.setFixedHeight(40) self.register_button.setFixedHeight(40)
@ -100,6 +125,8 @@ class LoginView(QWidget):
self.login_switch.setFlat(True) self.login_switch.setFlat(True)
self.login_switch.clicked.connect(self.show_login_form) self.login_switch.clicked.connect(self.show_login_form)
self.reg_password_input.textChanged.connect(self.confirm_password_input.on_text_changed)
def show_login_form(self): def show_login_form(self):
self.is_registration = False self.is_registration = False
self.title.setText("Авторизация") self.title.setText("Авторизация")
@ -131,13 +158,13 @@ class LoginView(QWidget):
self.main_layout.addWidget(self.login_switch) self.main_layout.addWidget(self.login_switch)
def clear_form(self): def clear_form(self):
for field in [ self.login_input.clear()
self.login_input, self.password_input, self.password_input.clear()
self.name_input, self.reg_login_input, self.name_input.clear()
self.reg_password_input, self.confirm_password_input, self.reg_login_input.clear()
self.invite_code_input self.reg_password_input.clear()
]: self.confirm_password_input.clear()
field.setText("") self.invite_code_input.clear()
def clear_main_layout(self): def clear_main_layout(self):
while self.main_layout.count() > 2: # сохраняем title и spacing while self.main_layout.count() > 2: # сохраняем title и spacing
@ -154,25 +181,33 @@ class LoginView(QWidget):
else: else:
QMessageBox.warning(self, "Ошибка", "Неверный логин или пароль") QMessageBox.warning(self, "Ошибка", "Неверный логин или пароль")
def validate_confirm_password(self, text):
if text != self.reg_password_input.text():
return False, "Пароли не совпадают"
return True, ""
def handle_register(self): def handle_register(self):
# Trigger validation for all fields
self.name_input.on_text_changed(self.name_input.text())
self.reg_login_input.on_text_changed(self.reg_login_input.text())
self.reg_password_input.on_text_changed(self.reg_password_input.text())
self.confirm_password_input.on_text_changed(self.confirm_password_input.text())
self.invite_code_input.on_text_changed(self.invite_code_input.text())
if not all([
self.name_input.is_valid,
self.reg_login_input.is_valid,
self.reg_password_input.is_valid,
self.confirm_password_input.is_valid,
self.invite_code_input.is_valid
]):
QMessageBox.warning(self, "Ошибка", "Пожалуйста, исправьте ошибки в форме")
return
name = self.name_input.text() name = self.name_input.text()
login = self.reg_login_input.text() # login = self.reg_login_input.text()
password = self.reg_password_input.text() # password = self.reg_password_input.text()
confirm = self.confirm_password_input.text() # invite = self.invite_code_input.text()
invite = self.invite_code_input.text()
if not all([name, login, password, confirm, invite]):
QMessageBox.warning(self, "Ошибка", "Заполните все поля")
return
if password != confirm:
QMessageBox.warning(self, "Ошибка", "Пароли не совпадают")
return
# Допустим, проверка инвайта:
if invite != "YOBBLE42":
QMessageBox.warning(self, "Ошибка", "Неверный инвайт-код")
return
QMessageBox.information(self, "Успех", f"Регистрация прошла успешно для {name}") QMessageBox.information(self, "Успех", f"Регистрация прошла успешно для {name}")
self.show_login_form() self.show_login_form()

View File

@ -0,0 +1,81 @@
from PySide6.QtWidgets import QWidget, QLineEdit, QLabel, QHBoxLayout, QVBoxLayout
from PySide6.QtCore import Qt, Signal
class ValidationInput(QWidget):
textChanged = Signal(str)
def __init__(self, placeholder_text="", is_password=False):
super().__init__()
self.validator = None
self.is_valid = False
self.is_required = True
self.main_layout = QVBoxLayout(self)
self.main_layout.setContentsMargins(0, 0, 0, 0)
self.main_layout.setSpacing(5)
input_layout = QHBoxLayout()
self.input = QLineEdit()
self.input.setPlaceholderText(placeholder_text)
self.input.setFixedHeight(40)
if is_password:
self.input.setEchoMode(QLineEdit.Password)
# self.status_label = QLabel()
# self.status_label.setFixedSize(20, 20)
# self.status_label.setAlignment(Qt.AlignCenter)
# font = self.status_label.font()
# font.setPointSize(14)
# self.status_label.setFont(font)
input_layout.addWidget(self.input)
# input_layout.addWidget(self.status_label)
self.error_label = QLabel()
self.error_label.setStyleSheet("color: red;")
self.error_label.hide()
self.main_layout.addLayout(input_layout)
self.main_layout.addWidget(self.error_label)
self.input.textChanged.connect(self.on_text_changed)
def set_validator(self, validator, is_required=True):
self.validator = validator
self.is_required = is_required
def on_text_changed(self, text):
self.textChanged.emit(text)
if not self.validator:
return
if not self.is_required and not text:
self.is_valid = True
# self.status_label.clear()
self.error_label.hide()
return
is_valid, message = self.validator(text)
self.is_valid = is_valid
if is_valid:
# self.status_label.setText("✓")
# self.status_label.setStyleSheet("color: green;")
self.error_label.hide()
else:
# self.status_label.setText("✗")
# self.status_label.setStyleSheet("color: red;")
self.error_label.setText(message)
self.error_label.show()
def text(self):
return self.input.text()
def setText(self, text):
self.input.setText(text)
def clear(self):
self.input.clear()
# self.status_label.clear()
self.error_label.hide()
self.is_valid = False