Motivation
It would be very useful for Formisch to provide a way to dynamically replace the schema of an existing FormStore.
This is especially important for forms representing objects whose validation rules depend on external data or whose fields are dynamically defined.
For example, a form may contain custom attributes whose definition comes from a parent object:
{
name: string,
customAttributesDefinition: [
{
name: string,
label: string,
type: string
}
]
}
The validation schema may therefore need to change when the parent object changes.
Current limitation
Currently, the schema is provided when the form is created:
const form = useForm({
schema,
})
If the schema needs to change later, there does not appear to be a way to update it on an existing FormStore.
Using reset() does not solve this problem because it resets the form state and input, but does not replace the schema.
A workaround in Vue is to recreate the form/component, for example using a changing :key, but this also recreates the entire FormStore and is not ideal.
Proposed API
Add a setSchema() method:
setSchema(form, newSchema)
For example:
const schema = createSchema(parentAttributes)
setSchema(form, schema)
The method would replace the schema associated with the existing FormStore.
Expected behavior
When setSchema() is called, Formisch should:
- Replace the current schema with the new schema.
- Keep the existing form input and values.
- Keep the existing
FormStore instance.
- Update the field validation structure according to the new schema.
- Revalidate fields according to the configured validation strategy.
- Update
isValid and the associated errors accordingly.
For example:
const form = useForm({
schema: initialSchema,
validate: 'blur',
revalidate: 'input',
})
// External data changes
const newSchema = createSchema(newParentAttributes)
setSchema(form, newSchema)
The form would then continue using the same FormStore, but with the new validation rules.
Motivation
It would be very useful for Formisch to provide a way to dynamically replace the schema of an existing
FormStore.This is especially important for forms representing objects whose validation rules depend on external data or whose fields are dynamically defined.
For example, a form may contain custom attributes whose definition comes from a parent object:
The validation schema may therefore need to change when the parent object changes.
Current limitation
Currently, the schema is provided when the form is created:
If the schema needs to change later, there does not appear to be a way to update it on an existing
FormStore.Using
reset()does not solve this problem because it resets the form state and input, but does not replace the schema.A workaround in Vue is to recreate the form/component, for example using a changing
:key, but this also recreates the entireFormStoreand is not ideal.Proposed API
Add a
setSchema()method:For example:
The method would replace the schema associated with the existing
FormStore.Expected behavior
When
setSchema()is called, Formisch should:FormStoreinstance.isValidand the associated errors accordingly.For example:
The form would then continue using the same
FormStore, but with the new validation rules.