How to manage multi Text Field validation in material-ui react?

て烟熏妆下的殇ゞ 提交于 2020-04-18 05:31:48

问题


I am creating a form in Materia-UI and React.js, and I am making validation of two TextField. I want to validate both TextFields with same error and textHelper props. Below I have show a piece of code of my work. If somebody have an idea please share with me, it will be helpful.

const AddPost = ({error, helperText, handleInputChange, handleSubmit}) => {
  <div>
    <form onSubmit={handleSubmit}>
    <TextField value={titleValue} onChange={handleInputChange} helperText={helperText} error={error} name={"title"} />
    <TextField value={bodyValue} onChange={handleInputChange} helperText={helperText} error={error} name={"body"} />
    </form>
  </div>
}


class Post extends React.Component {
    constructor(props) {
      super(props);
        this.state = {
          body: '',
          title: '',
          helperText: "",
          error: false,
    }
  }

  handleInputChange = (event) => {
    this.setState({
      [event.target.name]: event.target.value
    })
    if (event.target.value < 1) {
      this.setState({
        error: true,
        helperText: "This field is required!",
      })
   } else {
      this.setState({
        error: false,
        helperText: "",
      })
    }
  }

  render(){
    return(
       <AddPost
          titleValue={this.state.title}
          bodyValue={this.state.body}
          handleInputChange={this.handleInputChange}
          handleSubmit={this.handleSubmit}
          helperText={this.state.helperText}
          error={this.state.error}
       />
    )
  }
}

Helper text is shown in both of TextFields even if I write only in first. Please help me to solve this issue, you will save my day.

来源:https://stackoverflow.com/questions/60797734/how-to-manage-multi-text-field-validation-in-material-ui-react

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!