我正在从 AntD 转移到 MaterialUI,并且不知道如何在不重新加载整个页面的情况下轻松实现表单验证和表单提交。
As an example ,点击“登录”后整个页面会重新加载,这对于 SPA 应用程序来说不是一个好主意。
我可以通过将 handleFunction 从元素移动到元素的 onClick 函数并从 Button 中删除 type="submit"以避免重新加载整个页面来绕过它。那行得通,但是它删除了验证,并且删除了用户单击“输入”以提交表单的能力。
知道如何实现吗?以下是我到目前为止的工作,但没有表单验证,也无法单击“输入”来提交表单:
return (
<Container component="main" maxWidth="xs">
<CssBaseline/>
<div className={classes.paper}>
<Avatar className={classes.avatar}>
<LockOutlinedIcon/>
</Avatar>
<Typography component="h1" variant="h5">
Sign in
</Typography>
<form className={classes.form}>
<TextField
variant="outlined"
margin="normal"
required
fullWidth
id="email"
label="Email Address"
name="email"
autoComplete="email"
autoFocus
errorMessages={['this field is required', 'email is not valid']}
onInput={e => setEmail(e.target.value)}
/>
<TextField
variant="outlined"
margin="normal"
required
fullWidth
name="password"
label="Password"
type="password"
id="password"
autoComplete="current-password"
onInput={e => setPassword(e.target.value)}
/>
<FormControlLabel
control={<Checkbox value="remember" color="primary"/>}
label="Remember me"
/>
<Button
fullWidth
variant="contained"
color="primary"
className={classes.submit}
onClick={onFinish}
>
Sign In
</Button>
<Grid container>
<Grid item xs>
<Link to={"/forgotpassword1"} variant="body2">
Forgot password?
</Link>
</Grid>
<Grid item>
<Link to={"/register"} variant="body2">
{"Don't have an account? Sign Up"}
</Link>
</Grid>
</Grid>
</form>
</div>
</Container>
);
最佳答案
你可以试试:
...
export default function SignIn() {
...
const [email, setEmail] = React.useState("");
const [password, setPassword] = React.useState("");
const [emailErrorText, setEmailErrorText] = React.useState("");
const [passwordErrorText, setPasswordErrorText] = React.useState("");
const onSubmit = e => {
e.preventDefault();
if (!email) {
setEmailErrorText("Please enter email");
} else {
setEmailErrorText("");
}
if (!password) {
setPasswordErrorText("Please enter password");
} else {
setPasswordErrorText("");
}
};
return (
<Container component="main" maxWidth="xs">
<CssBaseline />
<div className={classes.paper}>
<Typography component="h1" variant="h5">
Sign in
</Typography>
<form className={classes.form} noValidate>
<TextField
variant="outlined"
margin="normal"
required
type="email"
fullWidth
id="email"
label="Email Address"
name="email"
autoComplete="email"
autoFocus
value={email}
error={!!emailErrorText}
helperText={emailErrorText}
onChange={e => setEmail(e.target.value)}
/>
<TextField
variant="outlined"
margin="normal"
required
fullWidth
name="password"
label="Password"
type="password"
id="password"
autoComplete="current-password"
value={password}
error={!!passwordErrorText}
helperText={passwordErrorText}
onChange={e => setPassword(e.target.value)}
/>
<FormControlLabel
control={<Checkbox value="remember" color="primary" />}
label="Remember me"
/>
<Button
type="submit"
fullWidth
variant="contained"
color="primary"
className={classes.submit}
onClick={onSubmit}
>
Sign In
</Button>
<Grid container>
<Grid item xs>
<Link href="#" variant="body2">
Forgot password?
</Link>
</Grid>
<Grid item>
<Link href="#" variant="body2">
{"Don't have an account? Sign Up"}
</Link>
</Grid>
</Grid>
</form>
</div>
<Box mt={8}>
<Copyright />
</Box>
</Container>
);
}
关于javascript - 使用 Material UI 和 ReactJS 提交和验证表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62337231/