0
我願做同步驗證,但我不知道怎樣來獲得字段的對象獲取字段的在Redux的形式顯示校驗錯誤消息對象
validate.js
const validate = (values) => {
const errors = {};
if (!values.firstName) {
errors.firstName = 'Firstname is required';
} else if (values.firstName.length < 5 || values.firstName.length > 10) {
errors.firstName = 'Firstname must be between 5 - 10';
}
return errors;
}
export default validate;
SimpleReduxForm.js
import React, { Component } from 'react'
import { connect } from 'react-redux'
import { reduxForm, Field } from 'redux-form'
import validate from './validate'
const fields = [ 'firstName', 'lastName', 'age' ]
@reduxForm({
form: 'simpleReduxForm',
fields,
validate
})
export default class SimpleReduxForm extends Component {
render() {
const { handleSubmit, invalid, pristine, submitting } = this.props
const { fields } = this.props
console.log(fields)
return (
<div>
<form onSubmit={ handleSubmit(this.handleFormSubmit) }>
<Field name="firstName" component="input" type="text" />
<Field name="lastName" component="input" type="text" />
<Field name="age" component="input" type="number" />
<input type="submit" value="Submit" disabled={ pristine || invalid || submitting }/>
</form>
</div>
)
}
}
的輸出0從如下
上面的源代碼,這是
我看到sample coding從下面的文件只是一個數組不反對,但我不知道怎樣找我來工作
const { fields: { firstName, lastName } } = this.props
...
{ firstName.touched && firstName.error && <div>{ firstName.error }</div> }
請指教,謝謝
感謝@Gregg,我忘了提及我可以使用你所建議的編碼風格,但我很好奇爲什麼我不能這樣做[例子](https://github.com/erikras/react-redux-universal-hot-例如/斑點/主/ SRC /組件/ SurveyForm/SurveyFor m.js),謝謝 – Artisan
這個例子使用'redux-form @ 3.0.12',最新版本是'6.0.5',這就是爲什麼。如果你想使用3版主要版本,我想它會起作用。 – Gregg