提交表單後,我正在運行驗證並返回一組錯誤。我正在使用map將數組分解爲一組對象,但是,我無法確定如何使用this.state.errors
在正確的FormControl字段下顯示窗體上的錯誤。這裏的任何幫助都會很棒。如何使用React在窗體上顯示驗證錯誤
What errors look like after array is broken into
Object {name: "name.first", type: "required", value: undefined}
Object {name: "name.last", type: "required", value: undefined}
React component with form
getValidationState() {
var errors = this.state.errors;
if (!$.isEmptyObject(errors))
{
console.log("sefsefsdf");
errors.map(element => {
var errors = element;
});
}
if(errors) return 'error';
}
render() {
return (
<form onSubmit={this.handleSubmit.bind(this)}>
<FormGroup
validationState={this.getValidationState()} >
<FormControl
type="text"
name="firstName"
value={this.state.firstName}
placeholder="First name"
onChange={this.handleChange}
/>
<FormControl.Feedback />
{this.state.errors && <HelpBlock>{this.state.errors}</HelpBlock>}
</FormGroup>
<FormGroup >
<FormControl
type="text"
name="lastName"
value={this.state.lastName}
placeholder="Last name"
onChange={this.handleChange}
/>
</FormGroup>
<FormGroup >
<Button type="submit">
Save
</Button>
</FormGroup>
</form>
)
}
UPDATE
你想打印每個'HelpBlock'組件內部的錯誤嗎? – Chris
是的。因此,如果顯示的名字錯誤在firstName – bp123
下顯示錯誤消息,那麼這個錯誤數組可以包含它自己的錯誤,但是如果值爲'undefined',則不能顯示它? – Chris