使用react和material-ui进行表单验证

Bee*_*Nag 16 validation events reactjs material-ui

我目前正在尝试将验证添加到使用material-ui组件构建的表单.我有它的工作,但问题是,我目前正在进行的方式验证功能当前正在调用输入中的每个状态更改(即每个键入的字母).但是,我只希望在键入停止后进行验证.

鉴于我目前的代码:

class Form extends React.Component {

    state = {open: false, email: '', password: '', email_error_text: null, password_error_text: null, disabled: true};

    handleTouchTap() {
        this.setState({
            open: true,
        });
    }

    isDisabled() {
        let emailIsValid = false;
        let passwordIsValid = false;

        if (this.state.email === "") {
            this.setState({
                email_error_text: null
            });
        } else {
            if (validateEmail(this.state.email)) {
                emailIsValid = true
                this.setState({
                    email_error_text: null
                });
            } else {
                this.setState({
                    email_error_text: "Sorry, this is not a valid email"
                });
            }
        }

        if (this.state.password === "" || !this.state.password) {
            this.setState({
                password_error_text: null
            });
        } else {
            if (this.state.password.length >= 6) {
                passwordIsValid = true;
                this.setState({
                    password_error_text: null
                });
            } else {
                this.setState({
                    password_error_text: "Your password must be at least 6 characters"
                });
            }
        }

        if (emailIsValid && passwordIsValid) {
            this.setState({
                disabled: false
            });
        }
    }

    changeValue(e, type) {
        const value = e.target.value;
        const nextState = {};
        nextState[type] = value;
        this.setState(nextState, () => {
            this.isDisabled()
        });
    }

    login() {
        createUser(this.state.email, this.state.password);
        this.setState({
            open: false
        });
    }

    render() {

        let {open, email, password, email_error_text, password_error_text, disabled} = this.state;

        const standardActions = (
            <FlatButton
                containerElement={<Link to="/portal" />}
                disabled={this.state.disabled}
                label="Submit"
                onClick={this.login.bind(this)} 
            />
        );

        return (
            <div style={styles.container}>
                <Dialog
                    open={this.state.open}
                    title="Enter Your Details to Login"
                    actions={standardActions}
                >
                    <span className="hint--right hint--bounce" data-hint="Enter in your email address">
                        <TextField
                            hintText="Email"
                            floatingLabelText="Email"
                            type="email"
                            errorText={this.state.email_error_text}
                            onChange={e => this.changeValue(e, 'email')} 
                        />
                    </span>
                    <br />
                    <span className="hint--right hint--bounce" data-hint="Enter your password">
                        <TextField
                            hintText="Password"
                            floatingLabelText="Password"
                            type="password"
                            errorText={this.state.password_error_text}
                            onChange={e => this.changeValue(e, 'password')} 
                        />
                    </span>
                </Dialog>
                <h1>VPT</h1>
                <h2>Project DALI</h2>
                <RaisedButton
                    label="Login"
                    primary={true}
                    onTouchTap={this.handleTouchTap.bind(this)} 
                />
            </div>
        );
    }
}

export default Form;
Run Code Online (Sandbox Code Playgroud)

有没有一种方法可以实现我想要的功能,而不需要对代码进行重大更改,还是需要完全重构?

任何帮助将非常感激

谢谢你的时间

iam*_*ain 13

当前的 Material-UI 版本不使用errorTextprop,但仍有一种方法可用于显示错误并将验证应用于 Material-UI 中的 TextField。

我们使用error(Boolean) 属性来表示是否有错误。进一步helperText在 Material-UI 中显示TextField的错误文本使用属性,只需向其提供您想要显示的错误文本。

这样做:

<TextField
  value={this.state.text}
  onChange={event => this.setState({ text: event.target.value })}
  error={text === ""}
  helperText={text === "" ? 'Empty!' : ' '}
/>
Run Code Online (Sandbox Code Playgroud)


Chr*_*ris 10

检查是否必须在一定延迟后进行?我认为在大多数情况下足够的解决方案是将代码分解一点.不要触发你的isDisabled()功能changedValue().而是让它在onBlur事件上运行.

试试这个:

<TextField
  hintText="Password"
  floatingLabelText="Password"
  type="password"
  errorText={this.state.password_error_text}
  onChange={e => this.changeValue(e, 'password')}
  onBlur={this.isDisabled} 
/>
Run Code Online (Sandbox Code Playgroud)

然后你的功能变成:

changeValue(e, type) {
    const value = e.target.value;
    const nextState = {};
    nextState[type] = value;
    this.setState(nextState);
}
Run Code Online (Sandbox Code Playgroud)


小智 8

最简单的就是调用form.reportValidity()form可以通过调用获得event.currentTarget.form