Usage Example
I see! Here's the corrected and detailed explanation in English using the exact example you provided:
Full Example: Password Validation with Custom Exception​
import 'package:fform/fform.dart';
import 'package:fform_validator/fform_validator.dart';
/// Custom exception for password validation.
class PasswordValidationException extends FFormException {
final bool isMinLengthValid;
final bool isSpecialCharValid;
final bool isNumberValid;
PasswordValidationException({
required this.isMinLengthValid,
required this.isSpecialCharValid,
required this.isNumberValid,
});
/// Returns `true` if the password meets all validation criteria.
bool get isValid => isMinLengthValid && isSpecialCharValid && isNumberValid;
}
/// A form field for password validation.
class PasswordField extends FFormField<String, PasswordValidationException> {
PasswordField(super.value);
/// Validates the password.
PasswordValidationException? validator(String value) {
final validator = FFormValidator(value);
return PasswordValidationException(
isMinLengthValid: validator.isMinLength(8),
isSpecialCharValid: validator.isHaveSpecialChar,
isNumberValid: validator.isHaveNumber,
);
}
}
Explanation of the Code:​
-
PasswordValidationException:
- This class extends
FFormException
and provides a custom exception for password validation. - It holds three boolean values:
isMinLengthValid
: Whether the password meets the minimum length (e.g., 8 characters).isSpecialCharValid
: Whether the password contains at least one special character.isNumberValid
: Whether the password contains at least one number.
- The
isValid
getter returnstrue
only if all validation conditions are satisfied, otherwise it returnsfalse
.
- This class extends
-
PasswordField:
- This class extends
FFormField<String, PasswordValidationException>
, where:String
represents the value of the password field.PasswordValidationException
is the custom exception type that will be returned if the validation fails.
- The
validator
method performs the validation:- It creates an instance of
FFormValidator
to check if the password meets the required conditions:isMinLength(8)
: Checks if the password is at least 8 characters long.isHaveSpecialChar
: Checks if the password contains at least one special character.isHaveNumber
: Checks if the password contains at least one number.
- It then returns a
PasswordValidationException
containing the results of each validation check.
- It creates an instance of
- This class extends
Example Usage:​
Here's an example of how you would use the PasswordField
class in a form and handle validation:
void main() async {
// Create a PasswordField with an initial value
final passwordField = PasswordField('1234');
// Check if the password is valid
final isValid = passwordField.check();
if (isValid) {
print('Password is valid!');
} else {
final exception = passwordField.exception;
if (exception != null) {
// Print the validation results
print('Password validation failed:');
print('Min length valid: ${exception.isMinLengthValid}');
print('Special char valid: ${exception.isSpecialCharValid}');
print('Number valid: ${exception.isNumberValid}');
}
}
}
Detailed Explanation of Example:​
-
PasswordField Creation:
- We create an instance of
PasswordField
with an initial password value ('1234'
).
- We create an instance of
-
Validation Check:
- The
check()
method ofFFormField
is used to validate the password. This method runs thevalidator
function that you defined, and returns whether the password is valid or not.
- The
-
Handling Validation Failures:
- If the password is invalid, the exception (
PasswordValidationException
) will contain the result of each validation step (whether the password is long enough, contains special characters, or contains numbers). - You can then check the
exception
and print out which specific validation failed.
- If the password is invalid, the exception (
Summary:​
This approach allows you to build custom validation logic for your form fields, in this case, a password. You can create a custom exception to hold detailed error information and use it to manage and display validation results more effectively. The FFormValidator
class provides utility methods for validating common requirements like minimum length, special characters, and numbers.