Skip to content

Commit

Permalink
fix(example): update validation to throw on empty values
Browse files Browse the repository at this point in the history
  • Loading branch information
alestiago committed Sep 7, 2023
1 parent ab06b47 commit 6f6dcc0
Showing 1 changed file with 23 additions and 8 deletions.
31 changes: 23 additions & 8 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ class _MyFormState extends State<MyForm> {
labelText: 'Email',
helperText: 'A valid email e.g. [email protected]',
),
validator: (_) => _state.email.displayError?.text(),
validator: (value) => _state.email.validator(value ?? '')?.text(),
keyboardType: TextInputType.emailAddress,
textInputAction: TextInputAction.next,
),
Expand All @@ -143,7 +143,8 @@ class _MyFormState extends State<MyForm> {
labelText: 'Password',
errorMaxLines: 2,
),
validator: (_) => _state.password.displayError?.text(),
validator: (value) =>
_state.password.validator(value ?? '')?.text(),
obscureText: true,
textInputAction: TextInputAction.done,
),
Expand Down Expand Up @@ -188,7 +189,7 @@ class MyFormState with FormzMixin {
List<FormzInput<dynamic, dynamic>> get inputs => [email, password];
}

enum EmailValidationError { invalid }
enum EmailValidationError { invalid, empty }

class Email extends FormzInput<String, EmailValidationError>
with FormzInputErrorCacheMixin {
Expand All @@ -202,11 +203,17 @@ class Email extends FormzInput<String, EmailValidationError>

@override
EmailValidationError? validator(String value) {
return _emailRegExp.hasMatch(value) ? null : EmailValidationError.invalid;
if (value.isEmpty) {
return EmailValidationError.empty;
} else if (!_emailRegExp.hasMatch(value)) {
return EmailValidationError.invalid;
}

return null;
}
}

enum PasswordValidationError { invalid }
enum PasswordValidationError { invalid, empty }

class Password extends FormzInput<String, PasswordValidationError> {
const Password.pure([super.value = '']) : super.pure();
Expand All @@ -218,9 +225,13 @@ class Password extends FormzInput<String, PasswordValidationError> {

@override
PasswordValidationError? validator(String value) {
return _passwordRegex.hasMatch(value)
? null
: PasswordValidationError.invalid;
if (value.isEmpty) {
return PasswordValidationError.empty;
} else if (!_passwordRegex.hasMatch(value)) {
return PasswordValidationError.invalid;
}

return null;
}
}

Expand All @@ -229,6 +240,8 @@ extension on EmailValidationError {
switch (this) {
case EmailValidationError.invalid:
return 'Please ensure the email entered is valid';
case EmailValidationError.empty:
return 'Please enter an email';
}
}
}
Expand All @@ -238,6 +251,8 @@ extension on PasswordValidationError {
switch (this) {
case PasswordValidationError.invalid:
return '''Password must be at least 8 characters and contain at least one letter and number''';
case PasswordValidationError.empty:
return 'Please enter a password';
}
}
}

0 comments on commit 6f6dcc0

Please sign in to comment.