简体   繁体   English

Redux Form 7.4.2,如何防止表单在组件渲染时提交并显示错误

[英]Redux Form 7.4.2 , How to prevent form submit on component render and showing errors

Im using reduxForm 7.4.2 , working with client side validation on simple form.我正在使用reduxForm 7.4.2 ,在简单表单上使用客户端验证。

The form is being submitted on page load and shows error like below:表单在页面加载时提交并显示如下错误:

在此处输入图像描述

I want to prevent form being submitted on page load and show errors only when users click Submit button我想防止在页面加载时提交表单并仅在用户单击“提交”按钮时显示错误

Here is the form component using reduxForm:这是使用 reduxForm 的表单组件:

import React from 'react'
import { Field, reduxForm } from 'redux-form'

const renderField = ({ input, label, type, meta: { touched, error, warning } }) => {
  console.log(error);
  return(
    <div className="form-group">
      <label>{label}</label>
        <input {...input} placeholder={label} type={type} className="form-control" />
        {error && <div className="invalid-feedback d-block">{error}</div>}

    </div>
  )
}




const validate = values => {
    const errors = {}
    if (!values.name) {
      errors.name = 'Name is Required'
    } else if (values.name.length < 2) {
      errors.name = 'Must be 2 characters or more'
    }
    if (!values.email) {
      errors.email = 'Email is Required'
    } else if (!/^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i.test(values.email)) {
      errors.email = 'Invalid email address'
    }
    if (!values.password) {
      errors.password = 'Password Required'
    } else if (values.password.length < 6) {
      errors.password = 'Password must be 6 characters'
    }
    return errors
  }

const Form1Child=(props)=>{
  const { handleSubmit, pristine, reset, submitting } = props
  return (
    <form onSubmit={handleSubmit} className="needs-validation" noValidate>
      <Field name="name" type="text" component={renderField} label="Name"/>
      <Field name="email" type="email" component={renderField} label="Email"/>
      <Field name="password" type="password" component={renderField} label="Password"/>
      <div>
        <button className="btn btn-primary" type="submit" disabled={submitting}>Submit</button>
        <button className="btn btn-default" type="button" disabled={pristine || submitting} onClick={reset}>Clear Values</button>
      </div>
    </form>
  )
}

export default reduxForm({
    form: 'form1',
    validate 
  })(Form1Child)

Here is the parent component:这是父组件:

import React, { Component } from 'react'
import Form1Child from './Form1Child';

class Form1 extends Component {
    constructor(){
        super();
        this.state={

        }
    }

    handleSubmit=values=>{
        alert(JSON.stringify(values));
    }

  render() {
    return (
      <Form1Child onSubmit={this.handleSubmit}/>
    )
  }
}
export default Form1;

In your validate function, do the following:在您的验证函数中,执行以下操作:

throw new SubmissionError('There were errors.')

This will prevent the action这将阻止动作

@@redux-form/SET_SUBMIT_SUCCEEDED

Its happen because you have not checked field is dirty or not.它的发生是因为您没有检查字段是否脏了。 In your renderField component you haven't checked field is touched or not.在您的renderField组件中,您没有检查字段是否被触及。

In your renderField component replace this line在您的renderField组件中替换此行

{error && <div className="invalid-feedback d-block">{error}</div>}

with

{touched && error && <div className="invalid-feedback d-block">{error}</div>}

and it should work.它应该工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM