How to check if a field is validated? #2633
Replies: 8 comments 11 replies
-
@bluebill1049 Hey there, I am interested in this feature as well, is there any opportunity to add this to the code base or write some guidelines? |
Beta Was this translation helpful? Give feedback.
-
Based on FormiK It would be something like: import { UseFormMethods } from "react-hook-form";
function isFieldValid<TFieldValues, K extends keyof TFieldValues>(form: UseFormMethods<TFieldValues>, field: K) {
return form.formState.dirtyFields[field] && !form.formState.errors[field]
} I am new to |
Beta Was this translation helpful? Give feedback.
-
you can combined with dirty fields |
Beta Was this translation helpful? Give feedback.
-
Is there no official way of doing this at the moment? |
Beta Was this translation helpful? Give feedback.
-
This would be a really helpful feature. Mantine has a https://mantine.dev/form/use-form/#validation Would be nice if it could also accept an array of paths. |
Beta Was this translation helpful? Give feedback.
-
If you are using a resolver, you can use the control._executeSchema method : export const useInput = ({ name }: UseInputProps) => {
const [isValid, setValid] = useState(false);
const { control } = useFormContext();
const {
field,
formState
} = useController({
name,
control,
});
useFocusEffect(
useCallback(() => {
(async () => {
try {
const { errors } = await control._executeSchema([name]);
setValid(!errors[name]);
} catch (err) { }
})();
}, [field.value])
);
return {
field,
formState,
control,
isValid
}
} |
Beta Was this translation helpful? Give feedback.
-
So no updates until now? |
Beta Was this translation helpful? Give feedback.
-
It would be very helpful to check that a field is valid just like you can check if a field has an error. What is the best approach here? |
Beta Was this translation helpful? Give feedback.
-
Using the new
{ mode: 'isTouched' }
feature, I want to be able to show status icons alongside input fields. Green check mark if the field is valid, red x if the field is invalid.I know how to check whether the field is invalid using
errors.fieldName
. But how do I check whether the field has been validated? Lack of errors tells you that one of the following is true: the input is valid OR the input has not been validated yet.Basically, is there anything that will expose the validation state for a field?
Beta Was this translation helpful? Give feedback.
All reactions