【问题标题】:Get React to display individual error messages under form input field获取 React 以在表单输入字段下显示单个错误消息
【发布时间】:2017-10-11 07:38:40
【问题描述】:

我正在提交一个返回错误数组的表单,但我无法弄清楚如何让每个单独的错误出现在正确的输入字段下。现在所有错误都打印在每个输入字段下。我正在使用react-bootstrap。任何帮助将不胜感激。

getValidationState() {
  var errors = this.state.errors;
  if (!$.isEmptyObject(errors))
  {
    errors.forEach(function(error) {
      console.log("error:", error.name);
    });
  }
}

render() {
  const inputProps = {
    value: this.state.address,
    onChange: this.onChange,
  }
  return (
    <form onSubmit={this.handleSubmit.bind(this)}>
      <FormGroup
        validationState={this.getValidationState()} >
        <FormControl
          type="text"
          name="firstName"
          value={this.state.firstName}
          placeholder="First name"
          onChange={this.handleChange}
        />
        <FormControl.Feedback />
        {this.state.errors && this.state.errors.length &&
          <HelpBlock>
            {this.state.errors.map((error, i) => <p key={i}>HEllo{error.value}</p>)}
          </HelpBlock>
        }


      </FormGroup>
      <FormGroup >
        <FormControl
          type="text"
          name="lastName"
          value={this.state.lastName}
          placeholder="Last name"
          onChange={this.handleChange}
        />
        {this.state.errors && this.state.errors.length &&
          <HelpBlock>
            {this.state.errors.map((error, i) => <p key={i}>HEllo{error.value}</p>)}
          </HelpBlock>
        }
      </FormGroup>


      <FormGroup >
        <Button type="submit">
          Save
        </Button>
      </FormGroup>
    </form>
  )
}

【问题讨论】:

    标签: reactjs meteor react-bootstrap


    【解决方案1】:

    每个输入的错误消息应该单独存储和显示(也可以使用状态):

    constructor(props){
      super(props)
      this.state = {
          firstNameErr: '',
          lastNameErr: '',
      }
      this.getValidationState = this.getValidationState.bind(this);
    }
    
    getValidationState() {
      var errors = this.state.errors;
      if (!$.isEmptyObject(errors))
      {
        var firstErr = '';
        var lastErr = '';
        errors.forEach((error) => {
          console.log("error:", error.name);
          // Check each error to see which input it belongs to
          // NOTE: please also consider using error.name instead, if error.message is invalid, thanks!
          if(error.message.indexOf('firstName') != -1){
            firstErr = error.message;
          }
          if(error.message.indexOf('lastName') != -1){
            lastErr = error.message
          }     
    
        });
    
        this.setState({
          firstNameErr: firstErr,
          lastNameErr: lastErr,
        });
    
      }
    }
    
    render() {
      const inputProps = {
        value: this.state.address,
        onChange: this.onChange,
      }
      return (
        <form onSubmit={this.handleSubmit.bind(this)}>
    
          <FormGroup
            validationState={this.getValidationState()} >
            <FormControl
              type="text"
              name="firstName"
              value={this.state.firstName}
              placeholder="First name"
              onChange={this.handleChange}
            />
            <FormControl.Feedback />
            <HelpBlock>
              <p className="text-danger">{this.state.firstNameErr}</p>
            </HelpBlock>
          </FormGroup>
    
          <FormGroup >
            <FormControl
              type="text"
              name="lastName"
              value={this.state.lastName}
              placeholder="Last name"
              onChange={this.handleChange}
            />
            <HelpBlock>
              <p className="text-danger">{this.state.lastNameErr}</p>
            </HelpBlock>
          </FormGroup>
    
    
          <FormGroup >
            <Button type="submit">
              Save
            </Button>
          </FormGroup>
        </form>
      )
    }
    

    就是这样,如果它还不起作用,请在此处显示控制台中的一些错误,谢谢

    【讨论】:

    • 我不知道为什么会收到Uncaught TypeError: this.setState is not a function Array.forEach
    • 抱歉忘记绑定getValidationState了,刚在构造函数里加了,能不能再试试,谢谢
    • 刚刚修复:"errors.forEach((error) => {" 你能再试一次吗
    • 对不起。现在有这个错误。 setState(...): Cannot update during an existing state transition (such as within render` 或其他组件的构造函数)。渲染方法应该是 props 和 state 的纯函数;构造函数副作用是一种反模式,但可以移动到componentWillMount。`
    猜你喜欢
    • 2014-06-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多