在Redux表单中向组件公开错误值

时间:2017-05-16 01:08:34

标签: reactjs react-redux redux-form

我有一个使用redux格式的组件,并且验证如下:

const validate = values => {
  // Initialize errors constant.
  const errors = {};

  const testEmail = value =>
    value && !/^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i.test(value) ?
    errors.email = 'Invalid email address' : undefined;

  // Only run test if email exists.
  testEmail(values.email);

  // Show error if value is touched and not filled out.
  const required = ['email', 'team', 'gameID', 'season', 'winner', 'bet', 'contractName'];
  required.map( input => {

    if (!values[input]) {
      return errors[input] = 'Required'
    };

    return null;
  });

  return errors;
}

const SportsFormWithCSS = CSSModules(SportsForm, styles, { allowMultiple: true });

SportsForm = reduxForm({
  form: 'SportsForm',
  validate
})(SportsFormWithCSS);

有没有办法可以将错误值添加到道具中?我尝试了几个没有运气的实现。

0 个答案:

没有答案