christianalfoni/formsy-react

non field errors

Opened this issue · 4 comments

I'm wondering how should one support errors returned by the api that don't correspond to any single field.

For example, say you have a Credit Card form. All the input values pass client-side validation. However, the card is low on funds and an api error is returned. How should a formsy form display the api validation error?

In django it's called a non_field_error http://devdocs.io/django~1.9/ref/forms/api#django.forms.Form.non_field_errors It'd be nice to outline this in the examples.

Hi, did you find any answer to this? I'm struggling with the same.
For example, the API returns an error key 'all' which isn't mapped to any field, and some other field errors, but the updateInputsWithError will fail because of the all key that is not an actual field.

I tried building a custom input that will simply be a placeholder for the all key, but adding an error there would cause the form to be in a permanent invalid status since the input is invalid and there's no easy way to make it valid while at the same time contain an error.

I have not found a solution. I have had to make my component stateful and append any non_field_error to the component's state. Then in the render block I do

render() {
  const flashError = this.state.errors.map((e) => <div class="error">{e.message}</div>);
  return (
    {flashError}
    ...
  )
}

I feel like this is a common enough problem (not my solution necessarily) that it bears adding to the formsy examples.

Does that mean you can't use updateInputsWithError directly from the server response so you don't need to map each field to form field manually one by one? I was doing things this way so errors would be really easy to manage as long as name and error keys match (which happens by design)

I came with probably a very ugly work around, but I ended up extending the Formsy.Form class, and added a different updateInputsWithError method which is basically a copy paste of the original, but rather than raising an exception on invalid keys, they are all sent to the last input error field. So all the non field errors always end up at the bottom. Not a great solution but allows me to leave the code as it is for error - field mapping.

right, fundamentally formsy attempts to map an error to an input. But we are currently handling errors that don't map to any particular input. It'd be great to get some guidance on this usecase from the authors.