question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Adding validations for dynamic data in react

See original GitHub issue

Using react formal with my schema statically defined (and reused):

export const AddressSchema = yup.object({
  category: string.nullable(),
  full_name: string.nullable(),
  address1: string.required(),
  address2: string.nullable(),
  city: string.required().min(2).max(30),
  state_code: string.required().min(2).max(2),
  postal_code: string.required().min(5).max(13),
  country_code: string.default('US').required().min(2).max(3),
  default_billing: boolean,
  default_shipping: boolean
})
export const ContactSchema = yup.object({
  first_name: string.required(),
  last_name: string.required(),
  email: string.required().email(),
  mobile_phone: string.required().phone(),
  work_phone: string.phone().nullable(),
  home_phone: string.phone().nullable(),
  addresses: edges(AddressSchema).min(1)
})

I have a dynamic dataset provided that gives me viable subregions (or states). How can I tack on a dynamic test to state_code inside a render or componentWillMount?

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:7 (7 by maintainers)

github_iconTop GitHub Comments

6reactions
rosskevincommented, Jan 26, 2017

Working state validation - dependent on context and other field:

  state_code: string.required().min(2).max(2)
    .test('validState', '${path} is not a valid state',
      function (value) {
        if (!value || (value !== null && value.length === 0)) {
          return false
        }

        console.log('test options', this.options)
        const { context, parent: address } = this.options
        const addressRegions = context.addressRegions() 
        const regionCode = address.country_code
        const result = addressRegions.subregions[ regionCode ].includes(value)
        return result
      }),
0reactions
rosskevincommented, Jan 26, 2017

Ugh - can’t use arrow functions here if I want this. No problem, just confusion.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to validate on dynamic input fields using react
I have displayed some dynamic input fields for name input from an array and trying to add validations on it. Expectations:.
Read more >
How to Add Form Validation to React Dynamic Forms
To build the Yup schema, we first add the static fields to , from and subject for validation. Then we loop through the...
Read more >
Dynamic field validation in Reactjs | by Unisha Acharya
In this article, we will be validating the dynamic fields. Let's dive into the code. Now, moving forward from the point where we…...
Read more >
Create Dynamic Forms in React Using React Hook Forms
This article is about creating dynamic forms in React using React Hook Form and performing validation on data entered in form fields.
Read more >
Dynamic Form Validations with React Hooks - Medium
Dynamic Form Validations with React Hooks · First we loop through each configuration in the configuration list · Clone the configuration which is ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found