form_guard 0.0.1
form_guard: ^0.0.1 copied to clipboard
A lightweight and customizable Dart form validation package for email, passwords, required fields, length checks, matching values, and more.
Form Guard #
A lightweight and customizable form validation package for Dart and Flutter.
form_guard provides simple, reusable validators for common form fields such as email addresses, passwords, required fields, string lengths, and matching values.
Features #
- Required field validation
- Email validation
- Password validation
- Minimum length validation
- Maximum length validation
- Matching field validation
- Custom validation messages
- Works with Dart and Flutter
- Lightweight with no unnecessary dependencies
Installation #
Add form_guard to your pubspec.yaml:
dependencies:
form_guard: ^0.0.1
Then run:
dart pub get
For Flutter projects, you can use:
flutter pub get
Import #
import 'package:form_guard/form_guard.dart';
Usage #
Required Field #
final result = FormGuard.required('');
print(result);
// This field is required.
A valid value returns null:
final result = FormGuard.required('Jorj');
print(result);
// null
Email #
final result = FormGuard.email('test@example.com');
print(result);
// null
Invalid email:
final result = FormGuard.email('invalid-email');
print(result);
// Please enter a valid email address.
Password #
final result = FormGuard.password(
'Hello123',
minLength: 8,
requireUppercase: true,
requireLowercase: true,
requireNumber: true,
);
print(result);
// null
You can also require a special character:
final result = FormGuard.password(
'Hello123!',
requireSpecialCharacter: true,
);
Minimum Length #
final result = FormGuard.minLength('hello', 8);
print(result);
// Must be at least 8 characters.
Maximum Length #
final result = FormGuard.maxLength('Hello World', 5);
print(result);
// Must not exceed 5 characters.
Matching Values #
Useful for fields such as password confirmation:
final password = 'Hello123';
final confirmPassword = 'Hello123';
final result = FormGuard.matches(
confirmPassword,
password,
);
print(result);
// null
Custom Error Messages #
All validators support customizable error messages.
FormGuard.required(
'',
message: 'Please enter your name.',
);
For example:
FormGuard.email(
'invalid',
message: 'That email address does not look right.',
);
Flutter #
form_guard works directly with Flutter's TextFormField.
TextFormField(
decoration: const InputDecoration(
labelText: 'Email',
),
validator: FormGuard.email,
);
For configurable validators:
TextFormField(
obscureText: true,
decoration: const InputDecoration(
labelText: 'Password',
),
validator: (value) {
return FormGuard.password(
value,
minLength: 8,
requireUppercase: true,
requireNumber: true,
);
},
);
Complete Example #
import 'package:form_guard/form_guard.dart';
void main() {
final email = 'test@example.com';
final password = 'Hello123';
final confirmPassword = 'Hello123';
print('Email: ${FormGuard.email(email)}');
print('Password: ${FormGuard.password(password)}');
print(
'Passwords match: '
'${FormGuard.matches(confirmPassword, password)}',
);
final invalidEmail = 'test';
print('Invalid email: ${FormGuard.email(invalidEmail)}');
}
In form_guard, validators follow the standard Flutter validation convention:
nullmeans the value is valid.- A
Stringmeans validation failed and contains the error message.
Contributing #
Contributions, bug reports, and feature requests are welcome.
If you find an issue or have an idea for a new validator, feel free to open an issue or submit a pull request on GitHub.
License #
Form Guard is available under the MIT License. See the LICENSE file for more information.