Comments (11)
Well, the most common use-case for API errors is after invoking submit
method. I personally would expect explicit errors merge, but as @dcolucci pointed out, it is basically possible in validations using props. Sorry, missed this functionality (and I definitely think it should be reflected in documentation).
Thank you!
from form.
-
Well, validate is a synchronous operation. So errors outside of client-side validation should be added manually. Even with async validation we still can run into this problem – imagine a situation when we have to show message near the input, if time run off.
-
But internally I won't use any refs! The idea is that API errors are the same errors as we have client-side, and sometimes it is not just a notification, but some info relevant to field. So it'd be nice to have this ability to render any errors natively.
from form.
I notice validate
passes state and props in addition to form values. Maybe this can help us. Modifying your Address example slightly (purposefully verbose & pedantic),
const AddressForm = Form({
// here we pass state and props as well
validate: (values, state, props) => {
let streetError
if (props.apiStreetError) {
streetError = `we didn't find ${values.street} in our API!`
}
return {
street: streetError,
city: !values.city ? 'A city is required' : undefined,
state: !values.state ? 'A state is required' : undefined
}
}
})(() => {
return (
<div>
<Text
field='street'
placeholder='Street'
/>
<Text
field='city'
placeholder='City'
/>
<Text
field='state'
placeholder='State'
/>
</div>
)
})
And then somewhere else we could have something like
<AddressForm apiStreetError={true} />
Then potentially-async outside operations (talking to API, etc) could force a re-render of the form with relevant validation messages.
from form.
from form.
from form.
from form.
from form.
I haven't tried that nor any of the above, more just thinking out loud in response to original question. The above idea also relies on validate
being called when the component receives new props, which it seems like it should since componentWillReceiveProps
calls setFormState
which calls validate
.
from form.
from form.
Honestly at this point, whatever needs to happen (as long as it aligns well with the api in the long run) I can build in fairly quickly. Just let me know what the most painless path to your goal is and I'll make your wildest dreams come true haha
from form.
I tried that idea, and here is what happens:
- Validate runs and passes initially.
- Submit sends data to the API.
- The form takes new props, but nothing changes the view, because
validate
does not run oncomponentWillReceiveProps
and that's reasonable.
I think we need to add "errors" to the Form API, and make it gets merged by the form's errors
. so using ref
seems the only possible way.
from form.
Related Issues (20)
- Values in nested arrays do not get updated properly
- [Tracking] Refactors prior to 1.x
- Array fields do not run onChange validation HOT 2
- Doubly nested array subfield infers `never` for value type HOT 8
- Mismatch in array handling between useForm and FormData HOT 3
- Error using useState & form.setFieldValue HOT 7
- Add a way to display only one error for a field HOT 3
- Calling `createFormFactory<FormData>` and using a validator adapter throw TS errors HOT 1
- onChangeAsyncDebounceMs not properly debouncing HOT 1
- TanStack/Angular - Error when resetting the form HOT 1
- Missing error when validating via form options instead of via field HOT 3
- Factory `onSubmit` function not invoked whenever factory.useForm gets provided with `onSubmit` itself HOT 2
- Unable to read properties in nested array table HOT 2
- Nested Values Disappear when using StrictMode HOT 5
- onMount Error is not revalidated when the user changes the input HOT 2
- server actions example errors when using zod adapter HOT 1
- Cannot infer types on arrays of union of objects HOT 1
- Moving subfields of array field mix their values HOT 1
- Bad performance with 100+ rows in an array field HOT 4
- insertFieldValue replaces item instead of adding a new one and shift existing items
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from form.