当前位置: 首页 > 知识库问答 >
问题:

登录验证-获取验证错误

盛城
2023-03-14
import React, { useContext } from "react";
    import {
      BoldLink,
      BoxContainer,
      FormContainer,
      Input,
      MutedLink,
      SubmitButton,
    } from "./common";
    import { Marginer } from "../marginer";
    import { AccountContext } from "./accountContext";
    
    export function LoginForm(props) {
      const { switchToSignup } = useContext(AccountContext);
    
      return (
        <BoxContainer>
          <FormContainer>
          <div class="form-group">
                <Input 
                type="text" 
                name="email" 
                value={this.state.input.email}
                onChange={this.handleChange}
                class="form-control" 
                placeholder="Enter email" 
                id="email" />
    
                <div className="text-danger">{this.state.errors.email}</div>
                </div>
    
            <div class="form-group">
                <Input 
                type="password" 
                name="password" 
                value={this.state.input.password}
                onChange={this.handleChange}
                class="form-control" 
                placeholder="Enter password" 
                id="password" />
    
                <div className="text-danger">{this.state.errors.password}</div>
                </div>
    
          </FormContainer>
          <Marginer direction="vertical" margin={10} />
          <MutedLink href="#">Forget your password?</MutedLink>
          <Marginer direction="vertical" margin="1.6em" />
          <SubmitButton type="submit">Signin</SubmitButton>
          <Marginer direction="vertical" margin="1em" />
          <MutedLink href="#">
            Don't have an accoun?{" "}
            <BoldLink href="#" onClick={switchToSignup}>
              Signup
            </BoldLink>
          </MutedLink>
        </BoxContainer>
       
      );
    }
    
    
    export class validLogForm extends React.Component {
        constructor() {
        super();
        this.state = {
          input: {},
          errors: {}
        };
         
        this.handleChange = this.handleChange.bind(this);
        this.handleSubmit = this.handleSubmit.bind(this);
      }
         
      handleChange(event) {
        let input = this.state.input;
        input[event.target.name] = event.target.value;
      
        this.setState({
          input
        });
      }
         
      handleSubmit(event) {
        event.preventDefault();
      
        if(this.validate()){
            console.log(this.state);
      
            let input = {};
            input["email"] = "";
            input["password"] = "";
            this.setState({input:input});
      
            alert('Demo Form is submitted');
        }
      }
      
      validate(){
          let input = this.state.input;
          let errors = {};
          let isValid = true;
       
         
      
          if (!input["email"]) {
            isValid = false;
            errors["email"] = "Please enter your email Address.";
          }
      
          if (typeof input["email"] !== "undefined") {
              
            var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
            if (!pattern.test(input["email"])) {
              isValid = false;
              errors["email"] = "Please enter valid email address.";
            }
          }
      
          if (!input["password"]) {
            isValid = false;
            errors["password"] = "Please enter your password.";
          }
      
      
          if (typeof input["password"] !== "undefined") {
            if(input["password"].length < 6){
                isValid = false;
                errors["password"] = "Please add at least 6 charachter.";
            }
          }
      
      
          this.setState({
            errors: errors
          });
      
          return isValid;
      }
      render() {
        return (
            <form onSubmit={this.handleSubmit}>
            <AccountContext/>
            </form>
            );
        }
         
    }

错误:

TypeError:无法读取未定义的LoginForm c:/reactjs/hello-world/src/components/accountbox/LoginForm.js的属性“state”:23 2023 value={this.state.input.email}^24 onchange={this.handlechange}25 class=“form-control”26 placeholder=“enter email”

共有1个答案

邓子濯
2023-03-14

因为this.state不是loginform的一部分,所以出现了错误。您需要将state作为道具传递给LoginForm,对handlechange执行相同操作:

如果试图在validlogform中呈现loginform(使用大写)

<LoginForm onSubmit={this.handleSubmit} state={this.state} handleChange={this.handleChange}>

并将this用于loginform中的props:

<Input 
  type="text" 
  name="email" 
  value={props.state.input.email}
  onChange={props.handleChange}
  class="form-control" 
  placeholder="Enter email" 
  id="email" 
/>
<div className="text-danger">{props.state.errors.email}</div>
 类似资料:
  • 接口说明 验证是否登录 如需调用,请访问 开发者文档 来查看详细的接口使用说明 该接口仅开放给已获取SDK的开发者 API地址 GET /authcenter/api/login/v1.0.0/refreshPermissions 是否需要登录 是 请求字段说明 参数 类型 请求类型 是否必须 说明 token string header 是 当前登录用户的TOKEN 响应字段说明 无 响应成功示

  • 接口说明 验证是否登录 如需调用,请访问 开发者文档 来查看详细的接口使用说明 该接口仅开放给已获取SDK的开发者 如开启https功能,请求地址的协议应改为https,如:https://www.example.com/wish3dearth/api/access/v1.0.0/getLicenseInfo API地址 GET /authcenter/api/login/v1.0.0/refre

  • 我正在进行多重身份验证。我的前端有一个带有卡号和密码的登录表单。在auth.php中 在route.php中 在logincontroller中, 在loginModel.php中 当我尝试登录时,我收到此错误 EloquentUserProvider.php第110行中的错误异常:传递给Illumate\Auth\EloquentUserProvider的参数1::validateCreenti

  • 我有一个简单的登录模型类,几乎没有数据注释验证 View是一个部分View,如下所示: 我相信我已经做了验证消息显示所需的所有事情,但它没有,客户端验证也没有。在母版页的头部区域,我还包括了以下脚本 是什么导致了这个问题?解决办法是什么?

  • 使用,https://github.com/firebase/FirebaseUI-Android/tree/master/codelabs/chat作为登录的参考,我在键入时似乎遇到了问题 我只能键入Auth的提供者,而不能键入,为什么会这样,它提示我键入社会提供者。

  • 接口说明 获取验证码 如需调用,请访问 开发者文档 来查看详细的接口使用说明 该接口仅开放给已获取SDK的开发者 API地址 POST /api/server/1.0.0/getValidCode 是否需要登录 否 请求字段说明 无 响应字段说明 无 响应成功示例 {} 响应失败示例 {} 响应接受类型 image/jpeg;charset=UTF-8 响应状态码 HTTP状态码 原因 204 N