When using the useField hook, field-level validation using the validate property doesn't work.
const [field] = useField({ name, validate: () => 'not good' });
The validate prop should work just like its Field counterpart.
https://codesandbox.io/embed/formik-codesandbox-template-p5zc1
Manually registering the validate function does the trick for me:
useEffect(() => {
registerField(name, {
validate: () => 'not good'
});
return () => unregisterField(name);
}, [name, registerField, unregisterField]);
| Software | Version(s) |
| ---------------- | ---------- |
| Formik | 2.0.1-rc.9
| React | 16.8.6
| TypeScript | 3.5.2
| Browser | Chrome 75
| npm/Yarn | npm
| Operating System | Ubuntu 19.04
Ahh yes, good catch
On that point, I think it makes sense to register field only if validate function is specified. Or is there some other plan for a field registry besides validation?
This is fixed by https://github.com/jaredpalmer/formik/pull/1699, released in v2.0.1-rc.13
This is fixed by #1699, released in
v2.0.1-rc.13
Doesn't seem to be fixed still in 2.1.4 :'(
@avetisk this works fine for me in v2.1.4. Are you able to reproduce in CodeSandbox?
@avetisk this works fine for me in v2.1.4. Are you able to reproduce in CodeSandbox?
I moved back to RFF, but I still tried to recreate the issue on CodeSandbox and couldn't, so I guess I missed something there in my code.
So no worries, it works fine! :)
Awesome, @bummey please have a look and see if this fixes your issue. Thanks!
Works fine now, thanks!
Why is this behavior not documented here?
I ran into this page by searching how to do field-level validation using hooks.
@mordechaim you're free to open a PR to update the documentation
Most helpful comment
Why is this behavior not documented here?
I ran into this page by searching how to do field-level validation using hooks.