如何修复React Form输入不隐藏验证错误

时间:2019-12-15 03:45:07

标签: reactjs forms validation

我正在尝试在反应中进行香草形式的验证。但是,当输入的值不正确时,实现会在表单提交时显示错误。

这是我的代码:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

options = Options()
options.binary_location = "/path/to/your/chrome/browser"
driver = webdriver.Chrome(chrome_options = options, executable_path=r'/path/to.your/chrome/driver')

...

但是当我输入正确的值时,错误仍会在表单提交时显示。我在做什么错了?

1 个答案:

答案 0 :(得分:1)

在每次验证之前重置错误状态

  validate = () => {
    let memberIdError= '';
    let firstNameError= '';
    let lastNameError= '';
    let emailAddressError = '';

    if(!this.state.memberId){
        memberIdError = 'Member ID can\'t be empty';
    }

    if(!this.state.firstName){
        firstNameError = 'First Name can\'t be empty';
    }

    if(!this.state.lastName){
        lastNameError = 'Last Name can\'t be empty';
    }        

    if(!this.state.emailAddress.includes('@')) {
        emailAddressError= 'Email Address can\'t be empty';
    }

    if(memberIdError || firstNameError || lastNameError || emailAddressError){
        this.setState({memberIdError, firstNameError, lastNameError, emailAddressError });
        return false;
    }

    return true;
}

在每次验证中,我们都将错误状态重置为空字符串”