New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Recommended validation library #78
Comments
🏁Final Form is unopinionated. Yup should work fine. Here's a list of some that work with Redux-Form and should work fine with 🏁React Final Form. |
@erikras I'm struggling implementing validation with yup, could you please provide a simple example ? it would really help, thanks. |
This might help? <Form
onSubmit={this.submitHandler}
validate={async (values) => {
const validator = validationSchema(values)
try {
await validator.validate(values, { abortEarly: false })
} catch (err) {
const errors = err.inner.reduce((formError, innerError) => ({
...formError,
[innerError.path]: innerError.message
}), {})
return errors
}
}} |
Thanks for asking for help with |
Took the above (edit and the below) and turned it into a simple function. import {Schema} from 'yup';
import {set, get} from 'lodash';
export function makeValidate<T>(validator: Schema<T>) {
return async (values: T) => {
try {
await validator.validate(values, {abortEarly: false})
} catch (err) {
return err.inner.reduce((errors: any, {path, message}: any) => {
if (errors.hasOwnProperty(path)) {
set(errors, path, get(errors, path) + ' ' + message);
} else {
set(errors, path, message);
}
return errors;
}, {});
}
};
}
Hopefully this saves someone 10 minutes. Note that Also, if you're integrating with MUI, you can use your definitions to set the |
@lookfirst that reduce doesn't work with nested or field arrays, I used to do that way to, it is setting
|
Thanks @Noitidart Working flawlessly! |
I added this without the lodash dependency to my Material UI + RFF library... https://github.com/lookfirst/mui-rff#helpers |
Thanks @ziaulrehman40 ! There was a bug in that, the @lookfirst we should be able to remove the lodash dependency as final-form exports some helpers: |
Thanks @Noitidart ... that is helpful... I can use those instead of my own versions and save some bytes.... |
@Noitidart I tried them as a drop in replacement and my tests are failing. I'll have to dig into it deeper to see what is going on, but wanted to let you know that they don't seem to be a direct easy replacement. |
Thanks please share your final solution.
…On Tue, Nov 26, 2019 at 5:52 AM Jon Stevens ***@***.***> wrote:
@Noitidart <https://github.com/Noitidart> I tried them as a drop in
replacement and my tests are failing. I'll have to dig into it deeper to
see what is going on, but wanted to let you know that they don't seem to be
a direct easy replacement.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#78?email_source=notifications&email_token=ABQTZCLPW2MZDOD3RS726RDQVUSY7A5CNFSM4EJJSSUKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEFGCFIY#issuecomment-558637731>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/ABQTZCIUBYIWXSQTKUVYZKDQVUSY7ANCNFSM4EJJSSUA>
.
|
@Noitidart any news with a final solution? |
For anyone out there interested in using More than happy to add this to the docs if there's any interest. |
Hey there 👋 . What do you recommend for the validation library?
Formik recommends yup, just curious what final-form does recommend.
Keep up the good work ❤️
The text was updated successfully, but these errors were encountered: