Form validation on autofill #1882
Replies: 15 comments 13 replies
-
hmmm browser level autofill right? |
Beta Was this translation helpful? Give feedback.
-
My solution in this case was to use controlled inputs with useController |
Beta Was this translation helpful? Give feedback.
-
Expanding on @jfulse's comment, you can custom-trigger the field's validation on browser's autofill by using I suppose you could do the same with the See this CSB for an example. Make sure to open the app in a new window because browser's autofill is funky with the way CSB does embedded apps. browser-autofill-validation.mp4Cc: @purebase |
Beta Was this translation helpful? Give feedback.
-
If someone is still looking for a quick fix. Here is what worked for me Add this before calling
|
Beta Was this translation helpful? Give feedback.
-
I think the problem lies on your end, aren't you overriding onChange prop (which should come automatically from spreading register)? We've just had a same issue on our project and overridden onChange was the reason for fields not interacting on autofill. |
Beta Was this translation helpful? Give feedback.
-
Adding below way worked for me with uncontrolled components when the browser auto-fills and still we see required field errors on submit. onChange={(e) => {
setValue('field-name', e.target.value);
}} |
Beta Was this translation helpful? Give feedback.
-
I ended up having to force-rerender the form on each change 🤮 const [_, setFakeState] = useState<number>(0);
....
<input
{...register('fieldName', {
onChange: e => {
setValue('fieldName', e.target.value);
setFakeState(state => state + 1);
}
> |
Beta Was this translation helpful? Give feedback.
-
I figured out that after auto filling react-hook-form doesn't change touched fields list, but changes dirty fields. So I resolved by comparing this data and trigger all fields. useEffect(() => {
if (Object.keys(dirtyFields).length && !Object.keys(touchedFields).length) {
trigger();
}
}, [dirtyFields, touchedFields]); |
Beta Was this translation helpful? Give feedback.
-
Looks like all these methods with
import { usePreventAutofill } from 'react-declarative';
...
const preventAutofill = usePreventAutofill();
...
return (
<InputBase
...
{...preventAutofill}
/>
);
|
Beta Was this translation helpful? Give feedback.
-
i'm using the approach with useEffect and The problem is that when field is autofilled, it becomes dirty but not touched. And it's exactly the same state as when the user has focused the field for the first time and typed something in without blurring yet. There appears to be no way to solve this without changing validation mode to "onChange" which is not desired. Is there maybe some way for the library to mark the field as touched, if its value changed from the default one without any user actions? Seems like that would be the expected behaviour. Validation still happens, but because the field remains "untouched", the validation error message will not appear, until user focuses the invalid field and blurs it. This is very problematic for forms that disable the submit button until everything is valid. this is about autofilling multiple fields, e.g. a registration form - user clicks first name, selects their contact from 1Password, and autofill happens on multiple fields. Only first name will become "touched", because it's been focused and blurred. The validation mode i want to use is "onTouched". "onChange" works as expected but nagging the user before they are finished is not good UX. |
Beta Was this translation helpful? Give feedback.
-
My colleague @gnekich and I came up with this solution: In you component, under useForm hook add this block:
we are trying to detect which fields changed after the browser autofill, or standard user input, and run trigger only on them |
Beta Was this translation helpful? Give feedback.
-
The following solution works really well for me. In the parent compoent :
In the child component:
|
Beta Was this translation helpful? Give feedback.
-
What worked for me was focusing on inputs right after persisting the change with
|
Beta Was this translation helpful? Give feedback.
-
I figured out that it is because react hook form with register relies on DOM events that doesn't trigger by browsers with autofill. So I used control and added "onInput" handler with "onChange" from that control. I guess it will work with register, but in this case we need to add "setValue" and "trigger" to our custom input component's props and invoke in "onInput", that's why I used to go with control insted.
|
Beta Was this translation helpful? Give feedback.
-
As an alternative and more native solution i would suggest to change mode to "onSubmit" and allow user to get the error messages if any when he hits submit, since it will natively trigger anything in form whats needed without anything hacky... |
Beta Was this translation helpful? Give feedback.
-
Is there a way to force validation on inputs when they are autofilled?
I have tried using onChange instead of onBlur, but it doesn't trigger either event when autofill is used.
This is the current setup:
I have tried to get it to work in Firefox and Chrome, but I have not found a way to get it to work.
Beta Was this translation helpful? Give feedback.
All reactions