Replies: 1 comment
-
Hello While still waiting for an answer, I gave some thoughts to this again this morning and I told myself I don't think it's even a good idea to force this kind of implementation 😃. The solution I came around with was to simply "unbind" the link between the input field and RHF by giving the input field another name. So, I ended up with this const handleInputAddTag = (tag: string) => {
if (tag) {
if (tag.length > 10) {
return setError("tags", {
type: "required",
message: "Tags must be between 1 and 10 characters.",
});
}
handleAddTag(tag, (updatedTags) => {
setValue("tags", updatedTags);
setValue("tag", "");
clearErrors("tags");
});
} else {
setValue("tag", "");
}
};
/* Inside InputTag parent component */
const { formState: { errors } } = useFormContext();
<InputTag
type="text"
tags={tags}
// Created a new name
name={"tag"}
className="mt-1"
/**
* For accessibility to remain I just created a new prop error
* Because in my Input component by default I did errors[name]?.message
*/
error={errors?.tags?.message}
onAddTag={handleInputAddTag}
onRemoveTag={handleRemoveTag}
helperText="Press Enter to add a tag"
placeholder="e.g. javascript, reactjs"
/> |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Hello
I am trying to buld a tags input component with zod and RHF. The end goal is to be able to submit the form with the
tags
field being an array. But since (from my understanding of the library) RHF binds the name to the input field, even if I callsetValue('tags', tagsArray)
. The moment I start typing again it is changed back to astring
. Hence the validation failsonSubmit
In my code, since I have a nested input component I started by rapping it with a
FormProvider
which received the method properties fromuseForm
Below is the handleKeyDown method
This is my zod validation schema
Below is a screencast
Screencast.from.07-30-2024.03.52.29.PM.webm
I tried using the useController hook but when I use that resetting the input value (
currentTarget.value = "";
) doesn't work anymore.This is what I get
Screencast.from.07-30-2024.04.05.02.PM.webm
Thanks
Beta Was this translation helpful? Give feedback.
All reactions