javascript - 验证 Redux 表单 : Nested Values

标签 javascript reactjs redux redux-form redux-form-validators

我在 ReduxForm 中进行了验证,直到我必须验证名称嵌套的字段,即:location.coordinates[0]

此数据在 Redux Store 中看起来像这样:

"location" : {
    "type" : "Point",
    "coordinates" : [ 
        103.8303, 
        4.2494
    ]
},

当尝试使用验证此类字段时

方法 1

if (!values.location.coordinates[0]) {
    errors.location.coordinates[0] = 'Please enter a longtitude';
}

我收到错误:

TypeError: Cannot read property 'coordinates' of undefined

方法 2

if (values.location !== undefined) {
    errors.location.coordinates[0] = 'Please enter a longtitude';
    errors.location.coordinates[1] = 'Please enter a latitude';
}

我收到错误:

TypeError: Cannot read property 'coordinates' of undefined

问题:处理此类字段的正确方法是什么?

/src/containers/Animals/AnimalForm.js

import React, { Component } from 'react';
import { connect } from 'react-redux';
import { reduxForm, Field } from 'redux-form';
import { renderTextField } from './FormHelpers';

class AnimalForm extends Component {
    render() {
        return (
            <div>
                <form onSubmit={ this.props.handleSubmit }                     
                        <Field
                            label="Longitude"
                            name="location.coordinates[0]"
                            component={renderTextField}
                            type="text"
                        />                      
                        <Field
                            label="Latitude"
                            name="location.coordinates[1]"
                            component={renderTextField}
                            type="text"
                        />   
                </form>
            </div>
        )
    }

}

const validate = values => {
    let errors = {}

    if (values.location !== undefined) {
        errors.location.coordinates[0] = 'Please enter a longtitude';
        errors.location.coordinates[1] = 'Please enter a latitude';
    }
    if ((values.location !== undefined) && (values.location.coordinates !== undefined)) {
        errors.location.coordinates[0] = 'Please enter a longtitude';
        errors.location.coordinates[1] = 'Please enter a latitude';
    }

    return errors;
}

function mapStateToProps(state) {
    return { ... }
}

export default connect(mapStateToProps)(reduxForm({
    form: 'animal',
    validate
})(AnimalForm))

/src/containers/Animals/FormHelper.js

import React from 'react';
import { FormGroup, Label, Input, Alert } from 'reactstrap';

export const renderTextField = ({input, type, meta: {touched, error}, ...custom}) => (
    <div>
            <Label>{ label }</Label>
            <Input
                type={type}
                value={input.value}
                onChange={input.onChange}
            />
            {touched && error && <Alert color="warning">{ error }</Alert>}
    </div>
)

最佳答案

在验证方法中,如何构造初始错误对象,如下所示:

let errors = {
  location: {
    coordinates: []
  }
}

关于javascript - 验证 Redux 表单 : Nested Values,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52650033/

相关文章:

javascript - 在 nodejs 中,如果记录不存在,如何将其插入到 PostgreSQL 中?

Javascript请解释这段代码

reactjs - 每次返回同一页面时 getStaticProps 是否都获取数据?

javascript - jsx 中的奇怪条件渲染失败

javascript - HTML/CSS : How to fade the left and right edges of a div?

reactjs - 使用 redux 命名导出?

javascript - 如何更新不可变列表中的特定值?

javascript - 在指定范围内覆盖 document.write?

javascript - 对 Google 隐藏部分 HTML 页面

reactjs - 如何使用Electron和React在页面之间导航?