API
Member | Type | Description |
---|---|---|
FFormField | Constructor | Abstract base class constructor initializing a field with an initial value of type T . Automatically performs validation via check() upon creation. |
E? exception | Getter & Setter | The exception associated with the field after validation. null if valid. Updates listeners when changed. |
bool get isValid | Getter | Indicates whether the field's value is valid (true if valid). |
bool get isInvalid | Getter | Indicates whether the field's value is invalid (true if invalid). |
Future<bool> check() | Method | Validates the field by running the validator and updates the exception state. Returns true if the field is valid after validation. |
E? validator(T value) | Abstract Method | Abstract method for custom validation logic. Should return an exception of type E if validation fails, otherwise null . |
Details of Key Members​
-
FFormField
Constructor- Initializes the field with an initial value of type
T
. - Immediately calls the
check()
method to validate the initial value.
Example:
final field = MyFormField(initialValue);
- Initializes the field with an initial value of type
-
E? exception
Getter & Setter- Getter: Retrieves the current validation exception.
- Setter: Sets a new exception and notifies listeners if the exception changes.
Example:
if (field.exception != null) {
print('Validation error: ${field.exception}');
} -
bool get isValid
- Indicates the validity of the field's value based on the
exception
.
Example:
if (field.isValid) {
print('Field is valid');
} - Indicates the validity of the field's value based on the
-
bool get isInvalid
- Returns
true
when the field is invalid (opposite ofisValid
).
Example:
if (field.isInvalid) {
print('Field is invalid');
} - Returns
-
Future<bool> check()
- Validates the field by calling the
validator
method. - Updates the
exception
state and notifies listeners. - Returns
true
if the field is valid after validation.
Example:
final isValid = await field.check();
if (isValid) {
print('Field is valid');
} else {
print('Field is invalid');
} - Validates the field by calling the
-
E? validator(T value)
- Must be implemented in subclasses to provide custom validation logic.
- Should return an exception of type
E
if validation fails, ornull
if the value is valid.
Example:
MyException? validator(MyValue value) {
if (value.isValid()) {
return null;
} else {
return MyException('Invalid value');
}
}