Accessing errors for Field Groups #2061
Replies: 4 comments
|
Bump |
|
import { useStore } from '@tanstack/react-form'
const DateRangeFields = withFieldGroup({
defaultValues: { startDate: '', endDate: '' },
render: function Render({ group }) {
const errors = useStore(group.form.store, (state) => {
const startPath = group.getFormFieldName('startDate')
const endPath = group.getFormFieldName('endDate')
return [
...(state.fieldMeta[startPath as never]?.errors ?? []),
...(state.fieldMeta[endPath as never]?.errors ?? []),
]
})
return (
<div>
<group.AppField name="startDate">
{(field) => <DateInput field={field} />}
</group.AppField>
<group.AppField name="endDate">
{(field) => <DateInput field={field} />}
</group.AppField>
{errors.map((e, i) => (
<p key={i} role="alert">{String(e)}</p>
))}
</div>
)
},
})
For per-field errors instead of combined, split into two |
|
Why would |
|
The group does have its own store, but it's a derived one. The constructor calls That's intentional: field groups are a scoping convenience.
The ergonomics gap is real. The |
Uh oh!
There was an error while loading. Please reload this page.
Hello, right now, field groups have access to the state values of all of its fields using form.Subscribe. I was wondering if there were plans to also give access to all of the errors of the field group so that they can be displayed. The use case here is two Date app fields that form a DateRange and essentially wanting to display the errors for either fields outside of the definition of the AppField.
All reactions