Upgrading to v2
Update your dependencies
Update @formiz/core
and @formiz/validations
packages to 2.x.x
npm install @formiz/core@alpha @formiz/validations@alpha
Update React react
and react-dom
(also @type/react
and @type/react-dom
if your are using Typescript).
npm install react@latest react-dom@latest
npm install -D @types/react@latest @types/react-dom@latest
Upgrade <Formiz>
π onChange
renamed to onValuesChange
Upgrade useForm
New usage
useForm
has been split into 3 different hooks: useForm
, useFormContext
and useFormFields
.
π useForm
is still used for creating a new form like before. However, the subscribe
prop is gone, and the hook now only returns the form's state and actions.
β¨ useFormContext
is used to access the form's state and actions in a sub component. It will not create a new form so you can't connect it to a <Formiz>
component.
β¨ useFormFields
is used to access fields states (not only values).
- Use it at top level with the
connect
option (useFormFields({ connect: form })
) or in a sub component (without theconnect
option). - Use a selector to get only the values (
useFormFields({ selector: (field) => field.value })
). - Get only some fields with the
fields
options (useFormFields({ fields: ['myField'])
)
// Formiz v1
const MyForm = () => {
const form = useForm();
console.log({form.values}) // β Doesn't work anymore
return <Formiz connect={form}>
<MyChildren />
<MyField name="myField" />
<MyField name="myOtherField" />
</Formiz>;
};
const MyChildren = () => {
const form = useForm({ subscribe: { fields: ['myField'] } }); // β Doesn't get the form anymore
console.log({ form.values }) // β Doesn't work anymore
return <>
{/* your code here */}
</>;
};
// Formiz v2
const MyForm = () => {
const form = useForm();
const values = useFormFields({
connect: form,
selector: (field) => field.value, // β
Get only the values
});
console.log({ values }); // β
{ myField: 'my field value', myOtherField: 'my field value' }
return (
<Formiz connect={form}>
<MyChildren />
<MyField name="myField" />
<MyField name="myOtherField" />
</Formiz>
);
};
const MyChildren = () => {
const form = useFormContext(); // β
Works
const values = useFormFields({
fields: ["myField"],
selector: (field) => field.value,
}); // β
Works
console.log({ values }); // β
{ myField: 'my field value' }
return <>{/* your code here */}</>;
};
Form actions updates
π setFieldsValues
renamed to setValues
- π
keepPristine
option now defaults to true. - β
keepUnmounted
was removed.setValues
will now always behave as ifkeepUnmounted
wastrue
.
π invalidateFields
renamed to setErrors
π getFieldStepName
renamed to getStepByFieldName
π nextStep
renamed to goToNextStep
π prevStep
renamed to goToPreviousStep
Upgrade useField
Validations API changes
π rule
renamed to handler
π Validations don't run on falsy values by default anymore
- You don't need to check on every validation that you have a value (
null
,undefined
,''
orfalse
) if your field is not required - If you want to check falsy values (like Formiz v1), just pass the
checkFalsy
option totrue
. - Now validations are now executed against formatted value.
// Formiz v1
<MyField
name="myField"
validations={[
{
rule: (value) => !value || value === "something", // π« `!value ||` useless with v2
message: 'Value must be "something"',
},
]}
/>
<MyField
name="myOtherField"
validations={[
{
rule: (value) => value === "something", // π« Will not be trigger on falsy value with v2
message: 'Value is required and must be "something"',
},
]}
/>
// Formiz v2
<MyField
name="myField"
validations={[
{
handler: (value) => value === "something", // β
Only triggered if required pass
message: 'Value must be "something"',
},
]}
/>
<MyField
name="myOtherField"
validations={[
{
handler: (value) => value === "something",
message: 'Value is required and must be "something"',
checkFalsy: true, // β
Opt-in to check also falsy value
},
]}
/>
Field props renamed
π onChange
renamed to onValueChange
π debounce
renamed to debounceValidationsAsync
This will only debounce the async validations and not the value anymore. This prevents a lot of weird behaviors that were existing in v1. Now the value is handled by React v18 to optimize renders.
π asyncValidations
renamed to validationsAsync
Types changes
You can now type the value
of useField
by passing a type to the FieldProps
type. Also the otherProps
will now be typed correctly π
// β
Pass the value type here ----- π
const MyField = (props: FieldProps<string>) => {
const { value, setValue, otherProps } = useField(props);
//...
};
Not implemented yet in alpha
keepValue
validating.start()
andvalidating.end()
stateSubscription
foruseForm
anduseFormContext
At the moment, both
useForm
anduseFormContext
will always return the full form's state. ThestateSubscription
option will allow to only return parts of the state, in case you need to further optimize renders on heavy forms.