0
下面是redux-form的代碼。一切工作都很好,但我無法在span元素中顯示錯誤消息。redux-form(版本6.4.3)不顯示錯誤
import React from 'react'
import { Button } from 'react-bootstrap'
import { Field, reduxForm } from 'redux-form'
const validate = (values) => {
const errors = {}
if (!values.firstname) {
errors.firstname = 'Required'
}
return errors
}
const renderInput = (field) => (
<div>
<label>{field.placeholder}</label>
<div>
<input {...field.input}/>
{field.error && <span>{field.error}</span>}
</div>
</div>
)
@reduxForm({
form: 'addUserForm',
validate
})
export default class CreateUserForm extends React.Component {
render() {
const {addUser, handleSubmit} = this.props
return (
<form onSubmit={handleSubmit(addUser)}>
<Field type="text" placeholder="First name" component={renderInput} name="firstname" />
<Button type="submit" className="btn btn-success">Submit</Button>
</form>
)
}
}
我可以清楚地看到,驗證功能工作(見下面的屏幕截圖)
但沒有在<span></span>
元素,這意味着field.error沒有值。我也根本沒有收到錯誤信息。
有人知道這裏發生了什麼嗎?
感謝, 托馬斯
完美。現在起作用了。找不到V6的文檔。感謝那。 –
不客氣:) –
不錯的答案,@NagarajTantri。 –