我正在使用Redux Form版本6.4.3并且我正在尝试验证两个日期字段,以使'to'日期必须始终在'from'日期之前.
其他例子说我应该能够引用fields
数组,props
但是没有这样的数组.该form
state
有称为数组registeredFields
但是但那些刚刚似乎是形式{name: 'dob', type: 'Field'}
这是我的表单代码
import React from 'react' import DatePicker from 'react-bootstrap-date-picker' import moment from 'moment' import {Field, reduxForm} from 'redux-form' import {Form, Row, Col, Button, FormGroup, ControlLabel, FormControl, HelpBlock} from 'react-bootstrap' // validations const required = value => !value ? 'This field is required' : undefined const maxDate = max => value => value && moment(value).isAfter(max) ? `Must be before ${max}` : undefined const minDate = min => value => value && moment(value).isBefore(min) ? `Must be after ${min}` : undefined const renderDatepicker = ({ input, label, hint, showTodayButton, meta: { pristine, touched, warning, error } }) => { const validationState = pristine ? null : error ? 'error' : warning ? 'warning' : null return () } export default reduxForm({ form: 'MyForm', })(MyForm))} const MyForm = props => { const { error, handleSubmit, pristine, reset, submitting, fields } = props console.debug('fields', fields) // fields is undefined return ( {label} {pristine && !!hint && ( {hint} )} {touched && ( (error && ({error} ) ) || (warning && ({warning} )) )}
我感觉到我遗漏了一些明显的东西,因为我所看到的所有例子都期望fields
数组存在于props
.
还值得一提的是,Field
验证函数签名也是validate : (value, allValues, props) => error [optional]
如此,您实际上也可以在字段级验证中引用其他字段值.
我相信你可以这样做:
const maxDateValidationAdapter = (value, values) => maxDate(values.dateTo)(value); // Alternatively, if maxDate() is used only in this form you can just change its signature
请参阅字段文档(您可以传递给Field => validate的道具).