Formik: Pass values to validationSchema

Created on 11 Mar 2018  路  2Comments  路  Source: formium/formik

Bug, Feature, or Question?

Feature

Description

It would be nice to have access to the values in the YUP scheme in the validationSchema. Possible use cases: need to validate based on the current values in the form. For example, one numeric value must not exceed another numeric value.

Most helpful comment

You can always use validate function and call validateYupSchema function directly with your desired context. Here is an example using synchronous validation and custom context.

validate: (values: FormValues) => {
    const validationSchema = object().shape({
        ...
    });

    try {
        validateYupSchema<FormValues>(values, validationSchema, true, {myCustomContext: 'bla'});
    } catch (err) {
        return yupToFormErrors(err);
    }

    return {};
},

I suggest looking at how Formik itself uses these functions in the source code.

All 2 comments

You can always use validate function and call validateYupSchema function directly with your desired context. Here is an example using synchronous validation and custom context.

validate: (values: FormValues) => {
    const validationSchema = object().shape({
        ...
    });

    try {
        validateYupSchema<FormValues>(values, validationSchema, true, {myCustomContext: 'bla'});
    } catch (err) {
        return yupToFormErrors(err);
    }

    return {};
},

I suggest looking at how Formik itself uses these functions in the source code.

@latviancoder Yes, it can solve my current problem, I would certainly like to have a second FormValues argument in the function validationSchema, but it's also nice

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jaredpalmer picture jaredpalmer  路  3Comments

Jungwoo-An picture Jungwoo-An  路  3Comments

green-pickle picture green-pickle  路  3Comments

dearcodes picture dearcodes  路  3Comments

dfee picture dfee  路  3Comments