Formik: Get values in validationSchema for dynamic validation rules

Created on 30 Aug 2017  ยท  4Comments  ยท  Source: formium/formik

As well as accessing the props in validationSchema is it possible to get the current values? I'm wanting a way to define the validation dynamically based on the values selected by the user.

e.g.

validationSchema?: Schema | ((props: Props) => Schema)
// becomes
validationSchema?: Schema | ((values: Values, props: Props) => Schema)

happy to submit a PR if you're interested in this.

Most helpful comment

Yes it is thanks!

so if anyone is interested it's now

validationSchema: props => {
    return Yup.lazy(values => {      
      return Yup.object().shape({

whereas before it was

validationSchema: props =>    
    Yup.object().shape({

All 4 comments

Hi Dave

Is yup.lazy enough to solve your problem?
https://github.com/jquense/yup/blob/master/README.md#yuplazyvalue-any--schema-lazy
On Wed, 30 Aug 2017 at 00:23 Dave Timmins notifications@github.com wrote:

As well as accessing the props in validationSchema is it possible to get
the current values? I'm wanting a way to define the validation dynamically
based on the values selected by the user.

e.g.

validationSchema?: Schema | ((props: Props) => Schema)// becomes
validationSchema?: Schema | ((values: Values, props: Props) => Schema)

happy to submit a PR if you're interested in this.

โ€”
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
https://github.com/jaredpalmer/formik/issues/145, or mute the thread
https://github.com/notifications/unsubscribe-auth/AAFj60XxOsLDgusTVRG3AZ1eqX3_eHpIks5sdOPPgaJpZM4PG2Z2
.

Yes it is thanks!

so if anyone is interested it's now

validationSchema: props => {
    return Yup.lazy(values => {      
      return Yup.object().shape({

whereas before it was

validationSchema: props =>    
    Yup.object().shape({

๐Ÿ‘๐Ÿ‘๐Ÿ‘
On Wed, 30 Aug 2017 at 05:49 Dave Timmins notifications@github.com wrote:

Closed #145 https://github.com/jaredpalmer/formik/issues/145.

โ€”
You are receiving this because you commented.

Reply to this email directly, view it on GitHub
https://github.com/jaredpalmer/formik/issues/145#event-1227272474, or mute
the thread
https://github.com/notifications/unsubscribe-auth/AAFj64mgA77ie2nKnuvs1FEdfa16JLrNks5sdTAigaJpZM4PG2Z2
.

@davetimmins Sounds good!!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jordantrainor picture jordantrainor  ยท  3Comments

ancashoria picture ancashoria  ยท  3Comments

Jucesr picture Jucesr  ยท  3Comments

jaredpalmer picture jaredpalmer  ยท  3Comments

sibelius picture sibelius  ยท  3Comments