Skip to content

Form-Level Validation Rule #121

@eraps7

Description

@eraps7

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.

\\Something like
\\Form - Level validator
overide
ValidationError? validator(String value) {
  if (isAllSearchParametersEmpty(firstName: firstName, lastName: lastName, phone: phone)) {
    return AtLeastOneFieldNeededError();
  }
  //This idea
  // Directly call field validators here
  final phoneError = phone.validator();
  final firstNameError = firstName.validator();
  final lastNameError = lastName.validator();

  // Combine errors for a holistic form-level validation result
  return ... (logic to aggregate errors and return a representative error)
}
tiny code
/// ValidationError is an abstract class that is used to implement the builder pattern to create a validation error(s).
abstract class ValidationError {
  final String message;

  ValidationError(this.message);
}

I am happy create a pull request and discuss peoples thoughts on the best way to implement this

Metadata

Metadata

Assignees

No one assigned

    Labels

    featureA new feature or requestp3Issues that we currently consider unimportant

    Type

    No type

    Projects

    Status

    Needs Triage

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions