0

我正在尝试学习 mobx-react-form 和验证部分。我想要一个简单的电子邮件验证,并且我想用 mobx-react-form 来做。 下面给出的是我的代码

const formFields = {
  email: {
    type: 'text',
    placeholder: 'Your Email Address',
    rules: 'required|email|string|between:5,50',
    value: ''
  }
};

@observer
class form extends React.Component {
  getValidation(): MobxReactForm {
    const hooks: any = {};

    const plugins: any = {
      dvr: dvr(validatorjs)
    };

    const formOptions: any = {
      validateOnChange: true
    };

    return new MobxReactForm({ formFields }, { plugins, hooks, formOptions });
  }
  @observable
  private form: MobxReactForm = this.getValidation();
  render() {
    return (
      <div>
        <form>
          <div>
            <FormControl margin="normal" fullWidth>
              <TextField {...this.form.$('email').bind()} />
            </FormControl>
            <Button variant="contained" color="primary" type="submit">
              Primary
            </Button>
          </div>
        </form>
      </div>
    );
  }
}
export default form;

它显示了这个错误

4

1 回答 1

0

我认为您的字段键错误:

  // It should be `fields` instead of `formFields`
  return new MobxReactForm({ fields }, { plugins, hooks, formOptions });

代码沙盒

于 2021-06-16T15:38:37.503 回答