ant-design icon indicating copy to clipboard operation
ant-design copied to clipboard

Form Provider should allow error handling and scrolling to first error

Open khalibloo opened this issue 3 years ago • 4 comments

What problem does this feature solve?

In our case, we have a page with multiple sections and each section has a form in it. The page has an overall submit button that when clicked, should submit all the forms. If all forms successfully validate, we submit all the data to the server at once. If a single one fails, we need to scroll up to the first invalid field on the page. Most of these can be done through workarounds, but it would be nice if the Form Provider supported these.

What does the proposed API look like?

Form Provider could have the following additional props

  • scrollToFirstError (boolean): scrolls to the first invalid field within the first invalid form.
  • onFinishAll (function): gets called when all forms validate successfully upon submission.
  • onFinishFailed (function): gets called when one of the forms fails to validate upon submission.

Maybe a Form Provider instance could be provided which would have the following properties

  • submit (function): submits all forms within the form provider
  • validateForms (function): validates all forms, but does not submit them
  • reset (function): resets all fields within all forms

khalibloo avatar Jul 27 '22 11:07 khalibloo

Could you provide an example codesandbox for the Forms usage?

zombieJ avatar Jul 29 '22 02:07 zombieJ

Sure. here's an example https://codesandbox.io/s/hungry-pond-hildoh?file=/src/App.js

khalibloo avatar Jul 29 '22 12:07 khalibloo

Thanks for your example.

Seems it more like Form should provide a scoped validation ability since FormProvider should not take too deep into the form ability.

zombieJ avatar Aug 02 '22 06:08 zombieJ

That's an option, but it goes beyond just validation. Each subform can be independently submitted or reset. And for the sake of accessibility, each subform may need to have its own form element in the DOM to work well with native form features (screen reader support, enter to submit, button with type=submit, etc).

khalibloo avatar Aug 02 '22 10:08 khalibloo

btw, antd 5.8.6, when set scrollToFirstError, and once field error occurs, there will throw error in "useForm.js", image image image

swellee avatar Sep 05 '23 08:09 swellee

@zombieJ , could you pls take a look?

swellee avatar Sep 05 '23 08:09 swellee

You should create a separate issue for that. It is unrelated to this one.

khalibloo avatar Sep 05 '23 20:09 khalibloo

exactly, will do

swellee avatar Sep 06 '23 01:09 swellee