如何在draftjs中应用验证

时间:2017-05-24 07:11:09

标签: javascript reactjs react-bootstrap draftjs

我有一个表单,其中包含使用富文本编辑器草稿的电子邮件,主题和文本。我可以验证其他字段,但我如何验证draftjs编辑器。当我点击编辑器键入时,如果它被留空,它应该显示错误,否则成功就像附加截图中的那个。

这是我的代码

function FieldGroup({ validationState, ...props }) {
  console.log(props);
  return (
    <FormGroup validationState={validationState}>
      <ControlLabel>{props.label}</ControlLabel>
      <FormControl {...props} />
    </FormGroup>
  );
}

class EmailTemplate extends React.Component {
  state = {
    emailFrom: ''
  };

  handleChange = event =>
    this.setState({ [event.target.name]: event.target.value });
  render() {
    const { templateName, emailSubject, emailFrom } = this.state;
    return (
      <form>
        <FieldGroup
          id="formControlsText"
          name="emailFrom"
          type="email"
          label="Email From"
          placeholder="Enter Email From"
          onChange={this.handleChange}
          validationState={emailFrom ? 'success' : 'error'}
          required
        />
        <AdminEditor />
        <Button type="submit">
          Submit
        </Button>
      </form>
    );
  }
}

export default EmailTemplate;


render() {
  const { editorContent, contentState, editorState } = this.state;
  return (
    <div>
      <Editor
        editorState={this.state.editorState}
        initialContentState={rawContentState}
        wrapperClassName="home-wrapper"
        editorClassName="home-editor"
        onEditorStateChange={this.onEditorStateChange}
        toolbar={{
          history: { inDropdown: true },
          inline: { inDropdown: false },
          list: { inDropdown: true },
          link: { showOpenOptionOnHover: true },
          textAlign: { inDropdown: true },
          image: { uploadCallback: this.imageUploadCallBack }
        }}
        onContentStateChange={this.onEditorChange}
        placeholder="write text here..."
        spellCheck
      />
    </div>
  );
}

enter image description here

2 个答案:

答案 0 :(得分:0)

您可以使用draft.js的onBluronFocus鼠标事件。

    用户离开时,
  • onBlur句柄将检查编辑器的内容 编辑器,然后单击其他位置。如果编辑器内容为空,则为 将editorValidated的状态设置为false。

  • onFocus句柄将始终将editorValidated设置为true。所以那里 用户再次输入时没有错误。

Editor组件中,您可以使用editorValidated通过CSS类添加自定义样式。

    function FieldGroup({ validationState, ...props }) {
      console.log(props);
      return (
        <FormGroup validationState={validationState}>
          <ControlLabel>{props.label}</ControlLabel>
          <FormControl {...props} />
        </FormGroup>
      );
    }

    class EmailTemplate extends React.Component {
      state = {
        emailFrom: '',
        editorValidated:true,
      };

      handleChange = event =>
        this.setState({ [event.target.name]: event.target.value });
      render() {
        const { templateName, emailSubject, emailFrom } = this.state;
        return (
          <form>
            <FieldGroup
              id="formControlsText"
              name="emailFrom"
              type="email"
              label="Email From"
              placeholder="Enter Email From"
              onChange={this.handleChange}
              validationState={emailFrom ? 'success' : 'error'}
              required
            />
            <AdminEditor />
            <Button type="submit">
              Submit
            </Button>
          </form>
        );
      }
    }

    export default EmailTemplate;


    render() {
      const { editorContent, contentState, editorState } = this.state;
      return (
        <div>
          <Editor
            editorState={this.state.editorState}
            initialContentState={rawContentState}
            wrapperClassName="home-wrapper"
            editorClassName=`home-editor ${(this.state.editorValidated)?'validated','not-validated'}`
            onEditorStateChange={this.onEditorStateChange}
            toolbar={{
              history: { inDropdown: true },
              inline: { inDropdown: false },
              list: { inDropdown: true },
              link: { showOpenOptionOnHover: true },
              textAlign: { inDropdown: true },
              image: { uploadCallback: this.imageUploadCallBack }
            }}
            onFocus={()=>{this.setState({editorValidated:true})}}
            onBlur={()=>{this.setState({editorValidated:(this.state.editorState.getCurrentContent().getPlainText()!='')})}}
            onContentStateChange={this.onEditorChange}
            placeholder="write text here..."
            spellCheck
          />
        </div>
      );
    }

答案 1 :(得分:0)

您可以使用private void OnOpenClicked(object parameter) { Logger.Info(parameter?.GetType().Name); } 来检查用户是否在编辑器中输入了任何文本并相应地应用样式。