-
Notifications
You must be signed in to change notification settings - Fork 39
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Form-Level Validation Rule #121
Comments
Up |
Hi @eraps7 & @codakkk 👋 Thanks for opening this issue! Overall it seems like what you're proposing here is actually doable with the I'm going to label this as a P3 given that we don't have any use case driving this need but if either of you would like to help with an implementation and contribute it back I can find cycles from the VGV team to help review. |
Well taking the following as example: part 'exercise_form.freezed.dart';
@freezed
class ExerciseForm with _$ExerciseForm, FormzMixin {
const ExerciseForm._();
const factory ExerciseForm({
@Default(ExerciseInput.pure()) ExerciseInput exercise,
@Default(SetListInput.pure()) SetListInput sets,
@Default(ExerciseNoteInput.pure()) ExerciseNoteInput note,
}) = _ExerciseForm;
@override
List<FormzInput<dynamic, dynamic>> get inputs => [
exercise,
sets,
note,
];
}
@freezed
class SetForm with _$SetForm, FormzMixin {
const SetForm._();
const factory SetForm({
@Default(RepTypeInput.pure()) RepTypeInput type,
@Default(RepValueInput.pure()) RepValueInput value,
@Default(false) bool isAMAP, // As Many As Possible
@Default('') String note,
}) = _RepsForm;
@override
List<FormzInput<dynamic, dynamic>> get inputs => [
type,
value,
];
}
enum SetFormListInputError {
empty,
}
class SetFormListInput
extends FormzInput<List<SetForm>, SetFormListInputError> {
const SetFormListInput.pure([super.value = const []]) : super.pure();
const SetFormListInput.dirty(super.value) : super.dirty();
@override
SetFormListInputError? validator(List<SetForm> value) {
return value.isEmpty ? SetFormListInputError.empty : null;
}
} I have a SetForm which describes a form for a single set in an exercise. Then I have a @override
List<FormzInput<dynamic, dynamic>> get inputs => [
exercise,
sets,
for (final input in sets.value) ...input.inputs,
note,
]; |
Is your feature request related to a problem? Please describe.
Currently, the formz package excels at validating individual form fields and separating the validation logic. However, it lacks the ability to define a validation rule that applies to the entire form.
For instance, consider a use case where the user can look up a an employee by any 3 fields: phone number, first name, and last name. While formz allows validation for individual fields (e.g., ensuring phone number has 10 digits and names have at least two characters), there's no way to enforce that at least one field must be filled before submitting the search.
Describe the solution you'd like
One possible solution would involve introducing a mechanism to define form-level validation rules that can orchestrate the execution of individual field validators. In the example above, the form level validator would perform validation that least one of the three fields (phone number, first name, or last name) has a value before allowing form submission then to orchestrate the execution of individual field validators.
tiny code
I am happy create a pull request and discuss peoples thoughts on the best way to implement this
The text was updated successfully, but these errors were encountered: