简体   繁体   中英

Validate a Field with Redux-form

I try learn how to works with redux-form and I have a little probleme. For example, I have a Field component like that:

<Field
  name='individual.email'
  id="email"
  component={ renderInput }
  placeholder='test'
/>

In validate.js I have something like that:

 if ( !values.individual.email ) {
            errors.individual.email = 'Required'
        }

But I have the error :

TypeError: Cannot set property 'email' of undefined

If I change the name of my Field by only 'email' it is works fine.. Any idea of what I do wrong when my Field is 'individual.email' ?

errors.invididual.email is an object within the errors object, ie

errors = {
    individual: {
        email: 'Required'
    }
}

At the top of your validate() function, you probably have declared the errors object, but it doesn't know about the individual property on it. That is why the field being only email works - you have defined errors and are setting the email property on it to required .

I'm not sure if it works, but you would have to do something like:

const errors = {
    individual: {}
};

And then you could assign errors.individual.email = 'Required'

Changing the name field so it's not using a dot. Use it individualEmail would be the standard way of naming. You shouldn't use a . In names

try this

 if ( values.individual && !values.individual.email ) {
            errors.individual.email = 'Required'
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM