Stacked-Org / stacked

A Flutter application architecture created from real world scenarios
MIT License
977 stars 255 forks source link

[feature]: Overall form validator #1009

Closed bdairy closed 1 month ago

bdairy commented 1 year ago

Is your feature request related to a problem? Please describe.

I have a form with two optional fields, but the use should fill at least one field,, for example , Email address or Mobile number..

I have email and mobile regex validators,, but the form wont be valid until one value is filled and valid,,

We need a custom form validator.

Describe the solution you would like

My suggestion is to add a validator field to the form.. like this

@FormView(
  fields: [
    FormTextField(
      name: 'mobile',
      initialValue: null,
      validator: FormValidator.validateMobile,
    ),
    FormTextField(
      name: 'email',
      initialValue: null,
      validator: FormValidator.validateEmail,
    ),
  ],
validator: FormValidator.validateForm /// This should accept a map of the field values.. 
)

You can get the idea from the Reactive Forms of angular...

Additional Context

No response

ferrarafer commented 1 year ago

@bdairy I will add this as a potencial new feature but in the meantime I think you can do something like below:

CustomFormViewModel

Future<void> saveUser() async {
  if (hasEmailValidationMessage && hasMobileValidationMessage) return;

  // do your task here...
}
bdairy commented 1 year ago

@bdairy I will add this as a potencial new feature but in the meantime I think you can do something like below:

CustomFormViewModel


Future<void> saveUser() async {

  if (hasEmailValidationMessage && hasMobileValidationMessage) return;

  // do your task here...

}

Already created a custom validator function for that form, I appreciate you guys for considering our requests.

Best of luck