Last active
June 12, 2025 12:20
-
-
Save goncaze/e0c589f6a8b97c583d1aa2ed49c2bcc9 to your computer and use it in GitHub Desktop.
Resolvi brincar com essa história de como lidar com a validação e acabei chegando em uma espécie de rascunho de modelo.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import flet as ft | |
| class App: | |
| def __init__(self, page: ft.Page): | |
| self.page = page | |
| self.ttf_email = self.get_ttf_email() | |
| self.ttf_senha = self.get_ttf_senha() | |
| self.btn_submeter = self.get_btn_submeter() | |
| self.cln_layout = self.get_cln_layout() | |
| self.dlg_alerta = ft.AlertDialog() | |
| self.lst_avaliacao = self.get_lst_avaliacao() | |
| self.page.add( | |
| ft.SafeArea(self.cln_layout), | |
| ) | |
| def get_cln_layout(self) -> ft.Column: | |
| return ft.Column(controls=[self.ttf_email, self.ttf_senha, self.btn_submeter]) | |
| def get_ttf_email(self) -> ft.TextField: | |
| return ft.TextField( | |
| label="E-mail", | |
| ) | |
| def get_ttf_senha(self) -> ft.TextField: | |
| return ft.TextField( | |
| label="Senha!", | |
| password=True, | |
| ) | |
| def get_btn_submeter(self) -> ft.ElevatedButton: | |
| return ft.ElevatedButton(text="Submeter", on_click=self.evento) | |
| def evento(self, e): | |
| if self.avaliar_entradas(): | |
| self.dlg_alerta.content = ft.Text(value="Login bem sucedido!") | |
| else: | |
| self.dlg_alerta.content = ft.Text(value="Falha no login!") | |
| self.page.open(self.dlg_alerta) | |
| self.page.update() | |
| def avaliar_entradas(self) -> bool: | |
| resultado = True | |
| for controle, avaliador, msg_erro in self.lst_avaliacao: | |
| aprovado = avaliador(controle, msg_erro) | |
| resultado = aprovado if not aprovado else resultado | |
| return resultado | |
| # Versão com recursão | |
| # def avaliar_entradas(self, lista: list[tuple]) -> None: | |
| # if len(lista) > 0: | |
| # controle, avaliador, msg_erro = lista.pop() | |
| # avaliacao_atual = avaliador(controle, msg_erro) | |
| # proxima_avaliacao = self.avaliar_entradas(lista) | |
| # return avaliacao_atual and proxima_avaliacao | |
| # return True | |
| def avaliar_email(self, controle: ft.Control, msg_error: str) -> bool: | |
| if controle.value == "[email protected]": | |
| controle.error_text = "" | |
| return True | |
| else: | |
| controle.error_text = msg_error | |
| return False | |
| def avaliar_senha(self, controle: ft.Control, msg_error: str) -> bool: | |
| if controle.value == "123": | |
| controle.error_text = "" | |
| return True | |
| else: | |
| controle.error_text = msg_error | |
| return False | |
| def get_lst_avaliacao(self) -> list[tuple]: | |
| return [ | |
| (self.ttf_email, self.avaliar_email, "E-mail inválido"), | |
| (self.ttf_senha, self.avaliar_senha, "Senha inválida"), | |
| ] | |
| def main(page: ft.Page): | |
| App(page=page) | |
| if __name__ == "__main__": | |
| ft.app(target=main) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment