issue: Errors are not removed from array fields #9875
Replies: 32 comments 3 replies
-
Please use the following code to debug the issue first: resolver: async (data, context, options) => {
// you can debug your validation schema here
console.log('formData', data)
console.log('validation result', await anyResolver(schema)(data, context, options))
return anyResolver(schema)(data, context, options)
}, |
Beta Was this translation helpful? Give feedback.
-
I've updated the example with this debugging part. As you can see below, the schema correctly reports no error when removing an item, but RHF still shows the error: |
Beta Was this translation helpful? Give feedback.
-
Please take a look at the CSB, I have switched to the latest version and let me know if I am missing something Screen.Recording.2023-02-05.at.10.11.20.am.mov |
Beta Was this translation helpful? Give feedback.
-
related issue: #9868 |
Beta Was this translation helpful? Give feedback.
-
The problem is still present in your CSB fork: RHF.Bug.movNotice that the error correctly appears after adding the third element (the max is 2), but after removing it, the error remains. It looks like the |
Beta Was this translation helpful? Give feedback.
-
Your schema is still returning errors even with 2 items. |
Beta Was this translation helpful? Give feedback.
-
It's returning the error for the right fields: the array elements. There's no error for the array itself, but the RHF still shows an error at the array level: |
Beta Was this translation helpful? Give feedback.
-
Refer to the same reply: #9868 (comment) |
Beta Was this translation helpful? Give feedback.
-
Sorry, I still don't get it. Are you saying that RHF showing an error that no longer exists is right? As I have shown, the last revalidation resulted in no error at the array level, but it still keeps a state with an error from the past. Even using the |
Beta Was this translation helpful? Give feedback.
-
No, The error is still there under the field array key |
Beta Was this translation helpful? Give feedback.
-
No, there is no error under the field array key, please see: Bug.3.mov |
Beta Was this translation helpful? Give feedback.
-
Screen.Recording.2023-02-06.at.8.50.35.am.mov
|
Beta Was this translation helpful? Give feedback.
-
So, are you saying that an error from a parent field will only be removed once all nested field errors have been fixed, even though it no longer has errors? |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
Sorry, I didn't get what you meant by "pick up the errors by field array key". Do I have access to the path of the field that triggered the revalidation from the resolver? |
Beta Was this translation helpful? Give feedback.
-
No matter what I do, the error doesn't disappear. Even not returning any other error, RHF still shows the error: bug.4.mov |
Beta Was this translation helpful? Give feedback.
-
import React from "react";
import ReactDOM from "react-dom/client";
import { useFieldArray, useForm } from "react-hook-form";
import * as yup from "yup";
import { yupResolver } from "@hookform/resolvers/yup";
import "./styles.css";
const schema = yup.object().shape({
friends: yup
.array()
.of(
yup.object().shape({
firstName: yup.string().required("First Name is required")
})
)
.min(1)
.max(2)
});
function App() {
const { register, handleSubmit, control, formState, clearErrors } = useForm({
mode: "onChange",
reValidateMode: "onChange",
defaultValues: {
friends: []
},
resolver: async (data, context, options) => {
// you can debug your validation schema here
const { errors, values } = await yupResolver(schema)(
data,
context,
options
);
if (Array.isArray(errors.friends) && !formState.isSubmitted) {
return {
errors: {},
values
};
}
return { errors, values };
}
});
console.log("formState.errors", formState.errors);
const { fields, remove, append } = useFieldArray({
name: "friends",
control: control
});
const onSubmit = (data) => {
console.log("submit", data);
};
const addFriend = () => {
append({ firstName: "" });
clearErrors("friends");
};
const removeFriend = (index) => () => {
remove(index);
};
const clearFriends = () => {
remove();
};
return (
<form onSubmit={handleSubmit(onSubmit)}>
<p>{formState?.errors?.friends?.message}</p>
{fields.map((field, index) => {
return (
<fieldset key={field.id}>
<label>
First Name {index}:
<input type="text" {...register(`friends.${index}.firstName`)} />
</label>
<button type="button" onClick={removeFriend(index)}>
Remove
</button>
</fieldset>
);
})}
<button type="button" onClick={addFriend}>
Add Friend
</button>
<button type="button" onClick={clearFriends}>
Clear Friends
</button>
<input type="submit" />
</form>
);
}
const root = ReactDOM.createRoot(document.getElementById("root"));
root.render(<App />); |
Beta Was this translation helpful? Give feedback.
-
Once you submit, the error never goes away: Bug.6.mov |
Beta Was this translation helpful? Give feedback.
-
I am just giving an example above, it's not the full implementation detail. |
Beta Was this translation helpful? Give feedback.
-
How can I make the error disappear without using No matter what I return from the resolver, once the error is shown, the only way to make it disappear is to trigger the full field validation, which I want to avoid. I don't see any other workaround besides these I've tried with no success. |
Beta Was this translation helpful? Give feedback.
-
Hey @bluebill1049 object-field-bug.mp4Check this sandbox I created mocking a real code I am working on: |
Beta Was this translation helpful? Give feedback.
-
Hello, is there any update on this issue? I'm also facing it, I've tried the following:
|
Beta Was this translation helpful? Give feedback.
-
Houston we have a big problem with |
Beta Was this translation helpful? Give feedback.
-
Apparently, it's a bug by design 🙃 |
Beta Was this translation helpful? Give feedback.
-
Just hit a similar bug. Not really sure how I can work around it. Even calling trigger again doesn't remove the 3rd row of error. Kinda hard to use an off the shelf array item component if I have to put a state-library-specific register method on every single input within that control. |
Beta Was this translation helpful? Give feedback.
-
One workaround that I've done in the recent past was to only render array errors if there aren't any items in it. {!fields.length && renderErrors()} I know it's not the ideal workaround, yet it does the job for my specific use case. Hope it helps anyone. |
Beta Was this translation helpful? Give feedback.
-
Same error here... I have an array of objects. I can use "remove" function from "useFieldArray()" to remove one of these objects. However, if the object i'm removing contains an error, this error is not being removed from "errors", and so i'm not able to submit the form, even if the object that contained the error no longer exist. One solution, as discussed, is to manually trigger, as this would update "errors"... but i think this should be automatic. |
Beta Was this translation helpful? Give feedback.
-
Hello, I have the same issue. Even my resolver doesn't return any errors. It still shows the last state of the error I received for the
Is there any proper solution for this issue? |
Beta Was this translation helpful? Give feedback.
-
can confirm even in the latest version |
Beta Was this translation helpful? Give feedback.
-
Can confirm in the latest version when using array of objects, errors still persist, only removed when re-clicking on the submit again. |
Beta Was this translation helpful? Give feedback.
-
Version Number
7.43.0/8-next.4
Codesandbox/Expo snack
https://codesandbox.io/s/react-hook-form-array-fields-with-validation-yup-forked-w9kye1?file=/src/index.js
Steps to reproduce
Expected behaviour
Revalidate the form once the item is removed to maintain consistency with the
append
behavior.Notice that using
trigger
is not an option since it will trigger the validation of all nested fields, which is undesired. In our case, we've deeply nested forms, and doing that results in a terrible UX.Bug.mov
What browsers are you seeing the problem on?
Chrome
Relevant log output
No response
Code of Conduct
Beta Was this translation helpful? Give feedback.
All reactions