使用reactjs验证对象数组
我正在使用ReactJS创建一个表单,其中输入字段将存储在数组和字符串中 如果字段是字符串,我可以验证,并且工作正常,如下所示:使用reactjs验证对象数组,reactjs,Reactjs,我正在使用ReactJS创建一个表单,其中输入字段将存储在数组和字符串中 如果字段是字符串,我可以验证,并且工作正常,如下所示: async handleSubmit(event) { event.preventDefault(); console.log('form submission data', this.state); if (this.state.harvest_type === "single_harvest" && t
async handleSubmit(event) {
event.preventDefault();
console.log('form submission data', this.state);
if (this.state.harvest_type === "single_harvest" && this.state.harvest_unit === "") {
this.setState({
alertMessage: "Please select Harvest unit",
showAlert: true,
alertcolor: "warning"
})
} else if (this.state.zone === "" || this.state.seeding_unit === "") {
this.setState({
alertMessage: "Please select zone & input metrics",
showAlert: true,
alertcolor: "warning"
})
}
{this.state.custom_fields.map((item1, idx) => (
<tr id={"nested" + idx} key={idx}>
<td>
<Row className="mb-2">
<Col md="10">
<Label className="col-form-label">{this.state.custom_fields[idx].name}<span style={{ color: "red" }}>*</span></Label>
<Input
type={this.state.custom_fields[idx].type}
id={"name_" + idx}
onChange={this.handleChange}
value={this.state.custom_fields[idx].value}
className="inner form-control"
required="required"
/>
</Col>
</Row>
</td>
</tr>
))}
我还有另一个动态输入字段,它存储在如下数组中:
async handleSubmit(event) {
event.preventDefault();
console.log('form submission data', this.state);
if (this.state.harvest_type === "single_harvest" && this.state.harvest_unit === "") {
this.setState({
alertMessage: "Please select Harvest unit",
showAlert: true,
alertcolor: "warning"
})
} else if (this.state.zone === "" || this.state.seeding_unit === "") {
this.setState({
alertMessage: "Please select zone & input metrics",
showAlert: true,
alertcolor: "warning"
})
}
{this.state.custom_fields.map((item1, idx) => (
<tr id={"nested" + idx} key={idx}>
<td>
<Row className="mb-2">
<Col md="10">
<Label className="col-form-label">{this.state.custom_fields[idx].name}<span style={{ color: "red" }}>*</span></Label>
<Input
type={this.state.custom_fields[idx].type}
id={"name_" + idx}
onChange={this.handleChange}
value={this.state.custom_fields[idx].value}
className="inner form-control"
required="required"
/>
</Col>
</Row>
</td>
</tr>
))}
{this.state.custom_fields.map((item1,idx)=>(
{this.state.custom_字段[idx].name}*
))}
我需要验证上述字段。如果用户没有输入,它应该抛出一条消息。我该怎么办?您想在阵列中验证哪些类型的数据?示例数据和预期结果的伪代码示例可能会使问题更加清楚。