验证材料 UI TextField 提交

时间:2021-01-03 10:37:31

标签: reactjs firebase material-ui

我正在尝试为登录的用户验证我的电子邮件和密码 TextField(s)。我能够通过我的 handleSubmit 函数捕获错误,但不确定如何将这些错误实现到 MaterialUI 中errorhelperText 字段。

请注意,我同时使用了 material-uireact-bootstrap,这就是它们混合使用的原因。

Login.js - 电子邮件和密码文本字段所在的位置

import React, { Component } from 'react';

import firebase from '../firebase';

import { FiLogIn } from 'react-icons/fi';

import Button from '@material-ui/core/Button';
import TextField from '@material-ui/core/TextField'

import Form from 'react-bootstrap/Form';
import Col from 'react-bootstrap/Col';

export class Login extends Component {
    state = {
        email : "",
        password : ""
    };
    handleChange = (e) => {
        const { id, value } = e.target
        this.setState(prevState => ({
            ...prevState,
            [id] : value
        }))
    };
    handleSubmit = (e) => {
        e.preventDefault();

        const { email, password } = this.state;
        firebase
        .auth()
        .signInWithEmailAndPassword(email, password)
        .then((user) => {
            // User is signed in
        })
        .catch((error) => {
            // Error
        });
    };
    render() {
        const { email, password } = this.state;
        return (
            <>
                <Form className="sign-in-form">
                    <Form.Row className="align-items-center">
                        <Col xs="auto">
                            <Form.Group controlId="email">
                                <Form.Label srOnly>Email Address</Form.Label>
                                <TextField
                                    id="email"
                                    label="Email"
                                    type="email"
                                    variant="outlined"
                                    aria-describedby="emailHelp"
                                    placeholder="Enter email"
                                    value={email}
                                    onChange={this.handleChange}
                                    
                                />
                            </Form.Group>
                        </Col>
                        <Col xs="auto">
                            <Form.Group controlId="password">
                                <Form.Label srOnly>Password</Form.Label>
                                <TextField
                                    id="password"
                                    label="Password"
                                    variant="outlined" 
                                    type="password"
                                    placeholder="Enter password"
                                    value={password}
                                    onChange={this.handleChange}
                                />
                            </Form.Group>
                        </Col>
                    </Form.Row>
                </Form>
                <Button variant="contained" color="primary" className="login" type="submit" onClick={this.handleSubmit}><FiLogIn className="loginIcon" /> Login</Button>
            </>
        )
    }
}

handleSubmit 函数 - 捕获 firebase 验证错误的位置

    handleSubmit = (e) => {
        e.preventDefault();

        const { email, password } = this.state;
        firebase
        .auth()
        .signInWithEmailAndPassword(email, password)
        .then((user) => {
            // User is signed in
        })
        .catch((error) => {
            // Error
        });
    };

让我知道我可以在这里做什么,我对 React 比较陌生,并且一直希望学习新事物。

2 个答案:

答案 0 :(得分:1)

为错误做一个状态:

state = {
    email : "",
    password : "",
    error:"",
};

在捕获错误时更改它:

    .catch((error) => {
        this.setState({error: error.response.data}) // change it to your error response 
    });

你的输入应该是这样的:

<FormControl error={error}>
    <InputLabel htmlFor="email">Email</InputLabel>
    <Input
      id="email"
      value={email}
      onChange={this.handleChange}
      aria-describedby="email"
    />
   <FormHelperText id="email"> {error ? error : "Enter your email address"}</FormHelperText>
</FormControl>

记得用handleChange清除错误状态。

答案 1 :(得分:1)

试试这个方法:

向组件添加 error 状态:

state =  {
        email : "",
        password : "",
        error : false,
        errMsg : "" 
    };

然后在 handleSubmit 内的 firebase auth 操作抛出错误时更改它:

.catch((error) => {
            this.state = {error : true, errMsg: error.msg};
        });

最后,添加一个条件 TextField 来显示错误信息:

{error &&    <TextField
              error
              id="yourErrorId"
              helperText=this.state.errMsg
              variant="outlined"
            />}