提交表单后,我正在运行验证并返回一组错误。我正在使用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