Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(example): update validation to report empty values #100

Merged
merged 2 commits into from
Sep 7, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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';
}
}
}