easy_forms_validation 0.3.0
easy_forms_validation: ^0.3.0 copied to clipboard
A Dart library that provides a mixin for form validation logic, allowing for easy validation of form data and management of form field validation state.
easy_forms_validation #
easy_forms_validation is a Dart library that allows for easy separation of form validation logic from the UI layer in Flutter applications.
It provides a simple and modular way to validate form data and manage form field validation state, making your code more maintainable and organized.
FormPart #
FormPart represents a part of the form that can be validated. It can be a field, a group of fields, or a whole form.
To validate any form part use bool validate() method.
FormControllerMixin #
FormControllerMixin is a FormPart that represent a form built from fields or other forms. It is responsible to perform validation on every its field.
Example:
enum EmailValidationError { invalidFormat, alreadyUsed }
enum PasswordValidationError { tooShort }
class LoginForm with FormControllerMixin {
final email = TextFieldController(
initialValue: '',
validator: (value, _) {
if (value.isEmpty || !value.contains('@')) {
return EmailValidationError.invalidFormat;
}
return null;
},
);
final password = TextFieldController(
initialValue: '',
validator: (value, _) {
if (value.length < 6) {
return PasswordValidationError.tooShort;
}
return null;
},
);
final consent = BoolFieldController();
@override
List<FormPart<FormPartState>> get fields => [email, password, consent];
}
FieldController #
FieldController is a FormPart that represents a field in a form.
Predefined types of field controllers:
TextFieldController- use it withTextFieldBuilderthat is used to control text field widgetBoolFieldController- use it when field value is type ofboolSelectFieldController- use it for fields that has predefined options, eg. dropdownMultiSelectFieldController- use it for fields that enables to select multiple predefined options
Extensions #
These mixins doesn't need to be a mixin on FormControllerMixin. They all require to implement FormController get form getter.
FormValueMixin- use it when you want to represent whole validated form as single value (object).SubmitFormMixin- use it with you want to add submit logic to the form