-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(example): update validation to throw on empty values
- Loading branch information
Showing
1 changed file
with
23 additions
and
8 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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, | ||
), | ||
|
@@ -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, | ||
), | ||
|
@@ -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 { | ||
|
@@ -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(); | ||
|
@@ -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; | ||
} | ||
} | ||
|
||
|
@@ -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'; | ||
} | ||
} | ||
} | ||
|
@@ -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'; | ||
} | ||
} | ||
} |