admin管理员组文章数量:1317898
I have created a form on which I want to validate the inputs and I saw the material UI have an attribute called error
boleean and another attribute called helperText
for TextField
input of the form but I didn't found nothing about how to inject that error on element when the validation conditon is not fulfiled
here is my code example: =/demo.js:1020-1055
const [form, setForm] = useState({ name: "", email: "", remember: "" });
const onChange = i => {
setForm({ ...form, [i.target.name]: i.target.value });
};
const handleSubmit = e => {
e.preventDefault();
console.log(form);
e.target.reset();
};
return (
<form className={classes.root} autoComplete="off" onSubmit={handleSubmit}>
<Grid container spacing={4}>
{Object.keys(form).map((objKey, idx) => {
return (
<Grid item xs={12} sm={6} md={4} key={idx}>
<TextField
error
helperText="No Value added in this field"
id={`input${idx}`}
label={objKey}
name={objKey}
fullWidth={true}
onChange={onChange}
/>
</Grid>
);
})}
<Grid item xs={12} sm={12} md={12}>
<Button type="submit" variant="contained" color="primary">
Submit
</Button>
</Grid>
</Grid>
</form>
);
I have created a form on which I want to validate the inputs and I saw the material UI have an attribute called error
boleean and another attribute called helperText
for TextField
input of the form but I didn't found nothing about how to inject that error on element when the validation conditon is not fulfiled
here is my code example: https://codesandbox.io/s/material-demo-hip84?file=/demo.js:1020-1055
const [form, setForm] = useState({ name: "", email: "", remember: "" });
const onChange = i => {
setForm({ ...form, [i.target.name]: i.target.value });
};
const handleSubmit = e => {
e.preventDefault();
console.log(form);
e.target.reset();
};
return (
<form className={classes.root} autoComplete="off" onSubmit={handleSubmit}>
<Grid container spacing={4}>
{Object.keys(form).map((objKey, idx) => {
return (
<Grid item xs={12} sm={6} md={4} key={idx}>
<TextField
error
helperText="No Value added in this field"
id={`input${idx}`}
label={objKey}
name={objKey}
fullWidth={true}
onChange={onChange}
/>
</Grid>
);
})}
<Grid item xs={12} sm={12} md={12}>
<Button type="submit" variant="contained" color="primary">
Submit
</Button>
</Grid>
</Grid>
</form>
);
Share
Improve this question
asked Apr 23, 2020 at 7:12
mcmwhfymcmwhfy
1,6866 gold badges36 silver badges60 bronze badges
1 Answer
Reset to default 6To use the error
prop of the TextField
you will need to manage the errors on your own (just like you manage the value for your fields).
To do that - the value of error
should not be fixed, but should be true/false, based on some calculation that you are doing.
I used your code to do the checking if the value (for each field) equals specific value. This is not a real-life example, and you probably want to change this to some kind of a regex check, but this should give you a good starting point:
export default function SubmitForm() {
const classes = useStyles();
const [form, setForm] = useState({ name: "aaa", email: "bbb", remember: "ccc" });
const isValidData = {name: "aaa", email: "bbb", remember: "ccc"};
...
const checkIsValid = (fieldName, value) => {
// Here you probably what to check this to some regex validation
return isValidData[fieldName] === value;
}
return (
...
{Object.keys(form).map((objKey, idx) => {
return (
<Grid item xs={12} sm={6} md={4} key={idx}>
<TextField
error={!checkIsValid(objKey, form[objKey])}
...
/>
</Grid>
);
})}
);
}
To see the plete working example please check this: https://codesandbox.io/s/mui-textfield-control-errors-z0tfo?file=/demo.js
本文标签: javascriptReact material UI form validation using error and helperTextStack Overflow
版权声明:本文标题:javascript - React material UI form validation using error and helperText - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742034855a2417151.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论