I don't really get the concept of the formz package in dart.
According to https://pub.dev/packages/formz
import 'package:formz/formz.dart';
// Define input validation errors
enum NameInputError { empty }
// Extend FormzInput and provide the input type and error type.
class NameInput extends FormzInput<String, NameInputError> {
// Call super.pure to represent an unmodified form input.
const NameInput.pure() : super.pure('');
// Call super.dirty to represent a modified form input.
const NameInput.dirty({String value = ''}) : super.dirty(value);
// Override validator to handle validating a given input value.
@override
NameInputError? validator(String value) {
return value.isEmpty ? NameInputError.empty : null;
}
}
There is a dirty
and a pure
representation of a form input, but why do i need this?
Isn't it just about validating the form input via the overwritten validator method? Therefore it would be enough to just have one constructor and run the validation?
I think i can answer it by myself. Dirty or clean form elements can be used as an indicator to decide for example if a save button is enabled or disabled in a form.
Imagine the following situation.
First Name -> John -> pure
Last Name -> Doe -> pure
Age -> 32 -> pure
And then check with
Formz.isPure([firstName, lastname, age])
will return true
and the save button should be disabled
Now, the age is changed to 33
First Name -> John -> pure
Last Name -> Doe -> pure
Age -> 33 -> dirty
And
Formz.isPure([firstName, lastname, age])
will be false and the button should be enabled
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With