Skip to main content

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:​

  1. 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 returns true only if all validation conditions are satisfied, otherwise it returns false.
  2. 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.

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:​

  1. PasswordField Creation:

    • We create an instance of PasswordField with an initial password value ('1234').
  2. Validation Check:

    • The check() method of FFormField is used to validate the password. This method runs the validator function that you defined, and returns whether the password is valid or not.
  3. 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.

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.