feat(form): add helpers
This commit is contained in:
parent
89f94cf1ff
commit
8909274543
@ -18,8 +18,13 @@ part of 'form_data_cubit.dart';
|
||||
|
||||
@immutable
|
||||
class FormDataState extends Equatable {
|
||||
/// Global status of a form.
|
||||
final FormStatus status;
|
||||
|
||||
/// FormData with all inputs, and associated metadata.
|
||||
final FormData data;
|
||||
|
||||
/// Optional error message.
|
||||
final String? errorMessage;
|
||||
|
||||
const FormDataState({
|
||||
|
@ -76,11 +76,21 @@ enum FormStatus {
|
||||
/// Indicates whether the form submission has been canceled.
|
||||
bool get isSubmissionCanceled => this == FormStatus.submissionCanceled;
|
||||
|
||||
/// Validate a list of inputs
|
||||
static FormStatus validate(List<FormInput> inputs) {
|
||||
return inputs.every((FormInput input) => input.validator.pure)
|
||||
/// Validate a list of inputs by processing them in `validate` as validators.
|
||||
static FormStatus validateInputs(List<FormInput> inputs) {
|
||||
return validate(
|
||||
inputs
|
||||
.map<FormInputValidator>((FormInput input) => input.validator)
|
||||
.toList(),
|
||||
);
|
||||
}
|
||||
|
||||
/// Validate a list of validators.
|
||||
static FormStatus validate(List<FormInputValidator> validators) {
|
||||
return validators.every((FormInputValidator validator) => validator.pure)
|
||||
? FormStatus.pure
|
||||
: inputs.any((FormInput input) => input.validator.valid == false)
|
||||
: validators
|
||||
.any((FormInputValidator validator) => validator.valid == false)
|
||||
? FormStatus.invalid
|
||||
: FormStatus.valid;
|
||||
}
|
||||
|
@ -23,8 +23,11 @@ class FormData extends Equatable {
|
||||
const FormData(this._inputs);
|
||||
const FormData.empty() : this(const <FormInput>[]);
|
||||
|
||||
/// Returns all inputs as a list
|
||||
List<FormInput> inputs() => _inputs;
|
||||
|
||||
/// Returns the input for the associated key
|
||||
FormInput inputByKey(String key) {
|
||||
FormInput inputOf(String key) {
|
||||
if (contains(key)) {
|
||||
return _inputs.firstWhere((FormInput input) => input.key == key);
|
||||
} else {
|
||||
@ -36,65 +39,83 @@ class FormData extends Equatable {
|
||||
void updateInput(String key, FormInput input) {
|
||||
if (contains(key)) {
|
||||
final index = _inputs.indexOf(
|
||||
inputByKey(key),
|
||||
inputOf(key),
|
||||
);
|
||||
_inputs[index] = input;
|
||||
}
|
||||
}
|
||||
|
||||
/// Updates validator of a given input. (perform copyWith)
|
||||
void updateValidator(String key, FormInputValidator dirtyValue) {
|
||||
if (contains(key)) {
|
||||
final index = _inputs.indexOf(
|
||||
inputByKey(key),
|
||||
);
|
||||
_inputs[index] = _inputs[index].copyWith(validator: dirtyValue);
|
||||
}
|
||||
}
|
||||
|
||||
/// Updates metadata of a given input. (perform copyWith)
|
||||
void updateMetadata(String key, FormInputMetadata meta) {
|
||||
if (contains(key)) {
|
||||
final index = _inputs.indexOf(
|
||||
inputByKey(key),
|
||||
);
|
||||
_inputs[index] = _inputs[index].copyWith(metadata: meta);
|
||||
}
|
||||
/// Returns all associated validators as a list
|
||||
List<FormInputValidator<V, E>> validators<V, E>() {
|
||||
return _inputs
|
||||
.map<FormInputValidator<V, E>>(
|
||||
(FormInput input) => input.validator as FormInputValidator<V, E>,
|
||||
)
|
||||
.toList();
|
||||
}
|
||||
|
||||
/// A [FormInputValidator] represents the value of a single form input field.
|
||||
/// It contains information about the [FormInputStatus], value, as well
|
||||
/// as validation status.
|
||||
T validatorOf<T>(String key) {
|
||||
return inputByKey(key).validator as T;
|
||||
return inputOf(key).validator as T;
|
||||
}
|
||||
|
||||
/// Returns a validation error if the [FormInputValidator] is invalid.
|
||||
/// Updates validator of a given input. (perform copyWith)
|
||||
void updateValidator(String key, FormInputValidator dirtyValue) {
|
||||
if (contains(key)) {
|
||||
final index = _inputs.indexOf(
|
||||
inputOf(key),
|
||||
);
|
||||
_inputs[index] = _inputs[index].copyWith(validator: dirtyValue);
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns a validation error if the [FormInputValidator] is invalid.
|
||||
/// Returns null if the [FormInputValidator] is valid.
|
||||
E? errorOf<V, E>(String key) {
|
||||
return (inputByKey(key).validator as FormInputValidator<V, E>).error;
|
||||
return (inputOf(key).validator as FormInputValidator<V, E>).error;
|
||||
}
|
||||
|
||||
/// The value of the associated [FormInputValidator]. For example,
|
||||
/// The value of the associated [FormInputValidator]. For example,
|
||||
/// if you have a FormInputValidator for FirstName, the value could be 'Joe'.
|
||||
V valueOf<V, E>(String key) {
|
||||
return (inputByKey(key).validator as FormInputValidator<V, E>).value;
|
||||
return (inputOf(key).validator as FormInputValidator<V, E>).value;
|
||||
}
|
||||
|
||||
/// Returns `true` if the [FormInputValidator] is not valid.
|
||||
/// Same as `E? errorOf<V, E>(String key) != null`
|
||||
bool isNotValid(String key) {
|
||||
return !inputByKey(key).validator.valid;
|
||||
return !inputOf(key).validator.valid;
|
||||
}
|
||||
|
||||
/// Returns all associated metadata as a list
|
||||
List<FormInputMetadata<M>> metadata<M>() {
|
||||
return _inputs
|
||||
.map<FormInputMetadata<M>>(
|
||||
(FormInput input) => input.metadata as FormInputMetadata<M>,
|
||||
)
|
||||
.toList();
|
||||
}
|
||||
|
||||
/// Returns the metadata associated. With `M` the type of extra data.
|
||||
FormInputMetadata<M> metadataOf<M>(String key) {
|
||||
return inputByKey(key).metadata as FormInputMetadata<M>;
|
||||
return inputOf(key).metadata as FormInputMetadata<M>;
|
||||
}
|
||||
|
||||
/// Updates metadata of a given input. (perform copyWith)
|
||||
void updateMetadata(String key, FormInputMetadata meta) {
|
||||
if (contains(key)) {
|
||||
final index = _inputs.indexOf(
|
||||
inputOf(key),
|
||||
);
|
||||
_inputs[index] = _inputs[index].copyWith(metadata: meta);
|
||||
}
|
||||
}
|
||||
|
||||
/// Validate self inputs.
|
||||
FormStatus validate() {
|
||||
return FormStatus.validate(_inputs);
|
||||
return FormStatus.validateInputs(_inputs);
|
||||
}
|
||||
|
||||
/// Check if this contains an input with the given key.
|
||||
@ -171,7 +192,7 @@ class FormData extends Equatable {
|
||||
|
||||
@override
|
||||
bool? get stringify => true;
|
||||
|
||||
|
||||
@override
|
||||
List<Object?> get props => _inputs;
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user