Created
March 18, 2023 14:15
-
-
Save Klerith/3e5cd6c543056ede3a7087bd6d6accf1 to your computer and use it in GitHub Desktop.
Flutter - Formz Inputs
This file contains 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 'package:formz/formz.dart'; | |
// Define input validation errors | |
enum EmailError { empty, format } | |
// Extend FormzInput and provide the input type and error type. | |
class Email extends FormzInput<String, EmailError> { | |
static final RegExp emailRegExp = RegExp( | |
r'^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$', | |
); | |
// Call super.pure to represent an unmodified form input. | |
const Email.pure() : super.pure(''); | |
// Call super.dirty to represent a modified form input. | |
const Email.dirty( String value ) : super.dirty(value); | |
String? get errorMessage { | |
if ( isValid || isPure ) return null; | |
if ( displayError == EmailError.empty ) return 'El campo es requerido'; | |
if ( displayError == EmailError.format ) return 'No tiene formato de correo electrónico'; | |
return null; | |
} | |
// Override validator to handle validating a given input value. | |
@override | |
EmailError? validator(String value) { | |
if ( value.isEmpty || value.trim().isEmpty ) return EmailError.empty; | |
if ( !emailRegExp.hasMatch(value) ) return EmailError.format; | |
return null; | |
} | |
} |
This file contains 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
export 'email.dart'; | |
export 'password.dart'; |
This file contains 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 'package:formz/formz.dart'; | |
// Define input validation errors | |
enum PasswordError { empty, length, format } | |
// Extend FormzInput and provide the input type and error type. | |
class Password extends FormzInput<String, PasswordError> { | |
static final RegExp passwordRegExp = RegExp( | |
r'(?:(?=.*\d)|(?=.*\W+))(?![.\n])(?=.*[A-Z])(?=.*[a-z]).*$', | |
); | |
// Call super.pure to represent an unmodified form input. | |
const Password.pure() : super.pure(''); | |
// Call super.dirty to represent a modified form input. | |
const Password.dirty( String value ) : super.dirty(value); | |
String? get errorMessage { | |
if ( isValid || isPure ) return null; | |
if ( displayError == PasswordError.empty ) return 'El campo es requerido'; | |
if ( displayError == PasswordError.length ) return 'Mínimo 6 caracteres'; | |
if ( displayError == PasswordError.format ) return 'Debe de tener Mayúscula, letras y un número'; | |
return null; | |
} | |
// Override validator to handle validating a given input value. | |
@override | |
PasswordError? validator(String value) { | |
if ( value.isEmpty || value.trim().isEmpty ) return PasswordError.empty; | |
if ( value.length < 6 ) return PasswordError.length; | |
if ( !passwordRegExp.hasMatch(value) ) return PasswordError.format; | |
return null; | |
} | |
} |
juank paez octubre 2024
Guillermo Núñez Noviembre 2024 xD
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Pablo Lara hizo este curso en septiembre del 2024