【问题标题】:Redux Form : How to add function after validation success and before submitRedux Form:验证成功后和提交前如何添加功能
【发布时间】:2016-08-24 13:48:39
【问题描述】:

我有一个经过验证的 redux 表单。当验证无误返回时,默认会在handleSubmit()中提交给控制器。

class SomeComponent extends Component {
  constructor(props) {
    super(props)
  }

  render() {
    const { handleSubmit, fields: { field }, submitting, values } = this.props;
    return (
    <form onSubmit={handleSubmit(this.props.someActionForm)}>
      <TextField {...field.input}
        type="text"
        label="FIELD"
        floatingLabelText="FIELD"
        errorText={field.touched? field.error: null}
      />

      <div className="col-xs-12">
        <FlatButton
          label="ACTIVATE"
          fullWidth={true}
          style={styles.fullButton}
          backgroundColor="#4FCDCC"
          hoverColor="#59e5e3"
          type="submit"
          disabled={submitting}
        />
      </div>
    </form>
   );
  }
}

function validate(values) {
  const errors = {};

  if(!values.field) {
    errors.field= "Field must not be empty !!!";
  }

  return errors;
}

SomeComponent = reduxForm({
  form: "SomeComponent",
  fields: ['field'],
  validate
}, mapStateToProps, mapDispatchToProps )(SomeComponent);

如何添加一个函数,例如在验证成功但表单提交到操作之前触发的加载元素(handleSubmit())?

提前谢谢你

【问题讨论】:

  • 在提交处理程序中有验证函数,并在验证为真后调用this.props. handleSubmit

标签: node.js reactjs redux material-ui redux-form


【解决方案1】:

你可以像这样添加一个类方法:

class SomeComponent extends Component {
  submit(formProps) {
    // your logic
    this.props.someActionForm(formProps);
  };

  render() {
    const {handleSubmit, fields: {field}, submitting, values} = this.props;
    return (
      <form onSubmit={handleSubmit(this.submit)}>
        <TextField {...field.input}
                   type="text"
                   label="FIELD"
                   floatingLabelText="FIELD"
                   errorText={field.touched ? field.error : null}
        />

        <div className="col-xs-12">
          <FlatButton
            label="ACTIVATE"
            fullWidth={true}
            style={styles.fullButton}
            backgroundColor="#4FCDCC"
            hoverColor="#59e5e3"
            type="submit"
            disabled={submitting}
          />
        </div>
      </form>
    );
  }
}

请注意,您在 render 方法中忘记了 return

【讨论】:

  • 是的,我忘记退货了。它在我的代码上,但我忘了把它放在这里。我将尝试使用您的方法附加提交,因为现在我正在使用从另一个来源获得的承诺请求方法。非常感谢
猜你喜欢
  • 1970-01-01
  • 2017-07-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-09
  • 2023-03-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多