How to update the form when a field update? #574
-
let say i have two fields: const form = useForm({
defaultValues: {
favoriteFood: "",
skip: false,
},
onSubmit: async ({ value }) => {
console.log(value);
},
});
that what i've tried: <form.Field
name="skip"
children={(field) => (
<Checkbox
checked={field.state.value}
name={field.name}
onChange={(e) => {
const isChecked = e.target.checked;
// Try 1: use the form.setFieldValue
field.form.setFieldValue("favoriteFood", isChecked ? "none" : "", { touch: true });
// Try 2: use the form.state.values
field.form.state.values.favoriteFood = isChecked ? "none" : "";
field.handleChange(isChecked)
}}
>
I don't have a favorite food
</Checkbox>
)}
/> I've tried using the any help is appreciated |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
So |
Beta Was this translation helpful? Give feedback.
So
form.setFieldValue("favoriteFood", "none")
is working as expected.in the end it was because i didn't pass the
field.state.value
to the right field of theCheckbox
(nextui), in the above code i passed it to thechecked
while it should have beenisSelected
.