redux-forms在提交前进行预验证

时间:2017-08-18 14:08:46

标签: reactjs react-redux-form

我想在提交之前验证表单并在底部显示常见错误,而不是将错误附加到特定字段。 errros.fieldname =“年龄> 10”。 如果没有错误,将提示消息继续提交,然后确认后将调用form-submit。

使用redux-forms的正确方法是什么?

roroco@roroco ~/Downloads/test/cs/create-nuget-dep-and-use-it/Dep $ lsb_release -a
No LSB modules are available.
Distributor ID: LinuxMint
Description:    Linux Mint 18 Sarah
Release:    18
Codename:   sarah

NuGet Version: 2.12.0.0
  
    

显示常见错误消息,而不是附加为error.text          渲染功能中的某个地方

  
 BeforeValidate(values)
  {
    if (<<some validation condition>>) {
        this.setState({err:"Error"})
        return;
    } else 
    {
        this.setState({DialogOpen:true, err:""})
    } 
  }

问题是BeforeValidate函数中未定义值。 redux-form中提供的synch validate不提供设置常见错误消息的方法。不确定asynch-validate是否正确。 所以我被抓住了。请帮忙。

1 个答案:

答案 0 :(得分:1)

您可以使用普通validate功能并添加_error。对于整个形式而言,这将是一个错误。

const validate = ({ options }) => {
  const errors = {};
  if (options.length === 0) errors._error = 'required';
  return errors;
};

在表单render中使用this.props.error检查表单错误。 像这样:

  render() {
    const { handleSubmit, onSubmit, submitting, error, submitFailed } = this.props;
    return (
      <View>
        {error &&
          <FormValidationMessage>
            {I18n.t(error)}
          </FormValidationMessage>}
      </View>
    );
  }