adr*_*nat 2 reactjs redux-form
在Redux-form v5中,我能够从装饰表单中的任何位置访问"内联"错误(异步验证),如下所示:
const fields = [
'email'
]
// inside the decorated form
const { email } = this.props.fields
console.log(email.error) // 'the validation error of the 'email' field
Run Code Online (Sandbox Code Playgroud)
如何使用Redux-form 6.0.0+实现相同的功能?
如果您想要在输入旁边显示错误,那么应该在component您传递给它的位置处理它Field.如果要一起显示所有错误,例如在提交按钮的表单底部,您可以使用新Fields组件,如下所示:
const fieldNames = [
'email',
'password'
]
const renderAllErrors = fields => (
<ul>
{Object.keys(fields).map(key => {
const { meta: { touched, error } } = fields[ key ]
return touched && error ? <li key={key}>{key}: {error}</li> : undefined
})}
</ul>
)
...
<Fields names={fieldNames} component={renderAllErrors}/>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1884 次 |
| 最近记录: |