FormField<T> class
When providing data to a form and then passing it forward, for instance,
in a request body, one problem that is common here is the need of dealing
with the cases where the field is not filled, and than one might need to
treat every possible resulting Map (json) separetily, either passing the not
filled field with no value or not passing it at all.
The generic sealed data class
FormField<Type>
is a convenience type that models, as the name already points,
a field in a Form, and uses the convention of not passing not filled fields to the resulting Map
.
But here we are already passing the name of the field
in its possible Map (json) position, and the actual field data is a Maybe<Type>
.
FormField
s are usually used in a Form
defined class, and with the usage of
our convenience mixin FormUtils
, one should have everything it needs to have
form validation, and toJson
method. It might introduce some verbose api, to
deal with, but the convenience of dealing with the most critical parts, like
validating and passing the Form
information through, makes the usage of our
FormField
s worthwhile.
example (using freezed
to create the Form
class):
@freezed
class FormExampleWithFreezed with _$FormExampleWithFreezed, FormUtils {
const FormExampleWithFreezed._();
const factory FormExampleWithFreezed({
@Default(FormField(name: 'firstFieldJsonName'))
FormField<String> firstField,
@Default(FormField(name: 'secondFieldJsonName'))
FormField<String> secondField,
}) = _FormExampleWithFreezed;
Result<String> get firstFieldValidation => validateField(
field: firstField.field,
validators: <String? Function(String)>[
// list of validators to first field
],
);
Result<String> get secondFieldValidation => validateField(
field: secondField.field,
validators: <String? Function(String)>[
// list of validators to second field
],
);
Map<String, dynamic> toJson() => fieldsToJson([
firstField,
secondField,
]);
}
Just to point out that the usage of a freezed class is not required to enjoy the advantages
of the FormField
type, we present another example(not using
freezed
):
class FormExample with FormUtils {
final FormField<String> firstField;
final FormField<String> secondField;
const FormExample({
required this.firstField,
required this.secondField,
});
Result<String> get firstFieldValidation => validateField(
field: firstField.field,
validators: <String? Function(String)>[
// list of validators to first field
],
);
Result<String> get secondFieldValidation => validateField(
field: secondField.field,
validators: <String? Function(String)>[
// list of validators to second field
],
);
Map<String, dynamic> toJson() => fieldsToJson([
firstField,
secondField,
]);
}
Using a Form
class as presented, one has a safe way to pass the values of
the field to a request body with easy.
Example:
request.body = formExampleInstance.toJson(),
- Annotations
-
- @freezed
Properties
-
copyWith
→ $FormFieldCopyWith<
T, FormField< T> > -
no setterinherited
-
field
→ Maybe<
T> -
Property representing the value of this FormField in a possible json request body
no setterinherited
- hashCode → int
-
The hash code for this object.
no setterinherited
- name → String
-
Property representing the name of this FormField in a possible json request body
no setterinherited
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
Methods
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited