import React from 'react'
import { Field, reduxForm } from 'redux-form'
const required = value => value ? undefined : 'Required'
const renderField = ({ input, label, type, meta: { touched, error, warning } })=> (
<div>
<label>{label}</label>
<div>
<input {...input} placeholder={label} type={type}/>
{touched && ((error && <span>{error}</span>) ||
(warning && <span>{warning}</span>))}
</div>
</div>
)
const FieldLevelValidationForm = (props) => {
const { handleSubmit, pristine, reset, submitting } = props
return (
<form onSubmit={handleSubmit}>
<Field name="username" type="text"
component={renderField} label="Username"
validate={[ required, maxLength15 ]}
/>
<div>
<button type="submit" disabled={submitting}>Submit</button>
<button type="button" disabled={pristine || submitting}
onClick={reset}>Clear Values</button>
</div>
</form>
)
}
export default reduxForm({form: 'fieldLevelValidation'})
(FieldLevelValidationForm)
当前实现缺少重复的用户名条目输入验证。我想知道如何定义逻辑来验证表单中输入的重复用户名字段?
您可以在此处使用reduxForm
中的asyncValidate
。此外,还应在验证中使用调度。你应该和下面一样做:
const asyncValidate = (values, dispatch) => {
//using dispatch here
}