【发布时间】:2020-09-30 15:17:53
【问题描述】:
我是新手,我正在使用来自验证的 material-ui,我试图通过获取 HTML 元素的 id 来验证表单。这很好,但每当我尝试验证下拉列表并发生错误时
无法读取未定义的属性“长度”。它无法读取“状态”的 ID。我该如何解决这个问题?
代码如下:
<TextField
label="Title"
variant="outlined"
size="small"
name="Title"
id="Title"
placeholder="Enter the Title of the Best Practice"
onChange={handleChange("Title")}
defaultValue={values.Title}
style={{ width: "80%" }}
/>
<label id="title" style={{ visibility: "hidden", color: "red" }}>
Title must be atleast 5 characters long
</label>
<br />
<TextField
placeholder="Enter the details of the Best Practice"
label="Details of Best Practice"
id="Details"
size="small"
name="Details"
onChange={handleChange("Details")}
defaultValue={values.Details}
style={{ width: "80%" }}
variant="outlined"
multiline
rows={4}
rowsMax={8}
/>
<label id="details" style={{ visibility: "hidden", color: "red" }}>
Details of Best Practice must be atleast 10 characters long
</label>
<br />
<TextField
placeholder="What is the Best Practice?"
label="What is the Best Practice"
size="small"
id="What"
name="What"
onChange={handleChange("What")}
defaultValue={values.What}
style={{ width: "80%" }}
variant="outlined"
multiline
rows={4}
rowsMax={8}
/>
<label id="what" style={{ visibility: "hidden", color: "red" }}>
What is the Best Practice must be atleast 10 characters long
</label>
<br />
<TextField
placeholder="Why was the Best Practice Implemented"
label="Why was the Best Practice Implemented"
size="small"
name="Why"
id="Why"
onChange={handleChange("Why")}
defaultValue={values.Why}
style={{ width: "80%" }}
variant="outlined"
multiline
rows={4}
rowsMax={8}
/>
<label id="why" style={{ visibility: "hidden", color: "red" }}>
Why was the Best Practice implemented must be atleast 10 characters
long
</label>
<br />
<TextField
placeholder="How was the Best Practice Implemented"
label="How was the Best Practice Implemented"
size="small"
name="How"
id="How"
onChange={handleChange("How")}
defaultValue={values.How}
style={{ width: "80%" }}
variant="outlined"
multiline
rows={4}
rowsMax={8}
/>
<label id="how" style={{ visibility: "hidden", color: "red" }}>
How was the Best Practice implemented must be atleast 10 characters
long
</label>
<br />
<FormControl id="Status" style={{ width: "80%" }} size="small">
<InputLabel
htmlFor="Implementation Status"
id="Status"
style={{
marginLeft: 10,
top: "50%",
transform: "translate(0,-50%"
}}
>
Implementation Status
</InputLabel>
<Select
labelId="Implementation Status"
name="Status"
id="Status"
onChange={handleChange("Status")}
defaultValue={values.Status}
variant="outlined"
inputProps={{
id: "Implementation Status",
name: "age"
}}
>
<MenuItem value="Implemented">Implemented</MenuItem>
<MenuItem value="Implementation in Progress">
Implementation in Progress
</MenuItem>
<MenuItem value="Not Implemented">Not Implemented</MenuItem>
</Select>
</FormControl>
<label id="status" style={{ visibility: "hidden", color: "red" }}>
Implementation Status cannot be blank
</label>
验证代码如下:
export class FormUserDetails extends Component {
continue = e => {
e.preventDefault();
if (document.getElementById("Title").value.length < 5) {
document.getElementById("title").style.visibility = "visible";
document.getElementById("Title").style.border = "1px solid red";
// keep form from submitting
} else if (document.getElementById("Details").value.length < 10) {
document.getElementById("details").style.visibility = "visible";
document.getElementById("Details").style.border = "1px solid red";
// keep form from submitting
} else if (document.getElementById("What").value.length < 10) {
document.getElementById("what").style.visibility = "visible";
document.getElementById("What").style.border = "1px solid red";
// keep form from submitting
} else if (document.getElementById("Why").value.length < 10) {
document.getElementById("why").style.visibility = "visible";
document.getElementById("Why").style.border = "1px solid red";
// keep form from submitting
} else if (document.getElementById("How").value.length < 10) {
document.getElementById("how").style.visibility = "visible";
document.getElementById("How").style.border = "1px solid red";
// keep form from submitting
} else if (document.getElementById("Status").value.length < 1) {
document.getElementById("status").style.visibility = "visible";
document.getElementById("Status").style.border = "1px solid red";
// keep form from submitting
} else {
// else form is good let it submit, of course you will
// probably want to alert the user WHAT went wrong.
this.props.nextStep();
}
};
直到状态的一切都正常。当我们到达 status 时,它会弹出错误
【问题讨论】:
-
您的
id的 inputLabel 和 Select 相同,您需要更改其中之一的id -
是的,但即使更改后它仍然会出错。你知道如何检查空下拉列表的值吗?
-
您的验证码是否无法访问状态变量
value? -
不需要
标签: javascript reactjs react-redux rxjs material-ui