简体   繁体   English

如何在reactjs中的材料ui TextField中进行错误验证?

[英]How to Error-validate in material ui TextField in reactjs?

I'm using material ui TextField in my react application.我在我的反应应用程序中使用材料 ui TextField。 I need to know how to use error and helper text validation for my below code which carry email and password?.我需要知道如何对下面带有电子邮件和密码的代码使用错误和辅助文本验证? Please go through the code given below and provide me a suitable solution.请仔细阅读下面给出的代码并为我提供合适的解决方案。

function LoginPage() {
  const handleSubmit1 = (event) => {
    event.preventDefault();
    const data = new FormData(event.currentTarget);
    console.log({
      email: data.get("email"),
      password: data.get("password"),
      remember: data.get("remember"),
    });
  };

return (
    <Box
      component="form"
      onSubmit={handleSubmit1}
      noValidate
      autoComplete="off"
      sx={{ mt: 1 }}
    >
      <FormControl sx={{ width: "60ch" }}>
        <CssTextField
          type="email"
          id="email"
          name="email"
          label="Username or email"
          variant="standard"
          fullWidth
          required
          autoComplete="off"
        />
        <CssTextField
          name="password"
          label="Password"
          type="password"
          id="password"
          autoComplete="off"
          variant="standard"
          fullWidth
          Validators
          required
        />
      </FormControl>
      <Stack direction="row" spacing={2}>
        <Button variant="contained" size="medium">
          LOGIN
        </Button>
      </Stack>
    </Box>
  );
}

I am not quite sure what exact problem you want to solve, can you elaborate a little more in details please.我不太确定您要解决的确切问题,请您详细说明一下。

I suppose you want to validate the input in some way and show error messages according to the validation state or just simply send the login request to your authentication service.我想您想以某种方式验证输入并根据验证状态显示错误消息,或者只是将登录请求发送到您的身份验证服务。 If my assumption is correct, you may want to make use of existing libraries, which will handle that for you - one of those is: mui-validate It will handle client side validation before sending any request to a server.如果我的假设是正确的,您可能想要使用现有的库,它会为您处理 - 其中之一是: mui-validate它会在向服务器发送任何请求之前处理客户端验证。

Or do you actually want to display the result of an async server response?或者您是否真的想显示异步服务器响应的结果?

After understanding your actual challenge better I am sure we can provide you with a good code example.在更好地了解您的实际挑战后,我相信我们可以为您提供一个很好的代码示例。

Try exploring this https://mui.com/material-ui/api/text-field/ here you have a list of all APIs MUI text-field supports if you are still not able to capture your scenario then try this https://mui.com/material-ui/react-text-field/ here you have demos of all possible text field scenarios with error ones as well, Try exploring the library documentation first as it will increase your knowledge and a better understanding of MUI package.尝试探索这个https://mui.com/material-ui/api/text-field/如果您仍然无法捕捉到您的场景,那么这里有 MUI 文本字段支持的所有 API 的列表,然后尝试这个https:/ /mui.com/material-ui/react-text-field/这里你有所有可能的文本字段场景的演示,也有错误的场景,首先尝试浏览库文档,因为它会增加你的知识和更好地理解 MUI 包. I hope with this one you can manage your case by yourself.我希望有了这个你可以自己处理你的案子。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM