【问题标题】:how to add different error to redux-form field on different action如何在不同的操作中向 redux-form 字段添加不同的错误
【发布时间】:2018-03-07 19:45:15
【问题描述】:

我的 redux 表单有两个动作,一个是 onclick,我只需要验证一个字段,在第二个动作中我需要提交动作。我怎样才能做到这一点

class BLoginForm extends React.PureComponent{
render(){
    const { handleSubmit, pristine, reset, submitting, onSubmit, openForgotForm } = this.props;
    return (
        <FormCon>
            <form onSubmit = { handleSubmit(onSubmit) }>
                <Field name = "username" type = "text" component = { BInputPhone } label = "Email or Mobile number" />
                <Field name = "password" type = "password" component = { BInput } label = "Password" />
                <a onClick = { openForgotForm } >forgot</a>
                <div>
                    <BPrimaryBtn type = "submit" disabled = { submitting }>LOGIN</BPrimaryBtn>
                </div>
            </form>
        </FormCon>
    );
}

}

【问题讨论】:

    标签: reactjs redux redux-form


    【解决方案1】:

    为了理解,我有 3 种不同的形式。

    function submitForms() {
      dispatch(submit('FirstOne'))  // First form
      dispatch(submit('secondOne')) // Second form
      dispatch(submit('thirdOne'))  // Thrid form
    }
    <Button type="button" onClick={ submitForms } />
    

    使用此功能,您将触发所有表单的提交功能。

    您可以实现每个提交以调度多个操作

    const mapstateToProps = (state,ownProps) => {
      {... do some stuff ... }
    
    return {
      submit : (values, dispatch, props) => {
             dispatch(YOUR ACTIONS) // So to validate one field ?
             dispatch(YOUR ACTIONS) // dispatch your second actions.
        }
     }
    
    
    export default Form = connect(mapStateToProps)(reduxForm({
      form:'YoURFORMNAME'
    })(YoURFORMCOMP))
    

    我可以补充一点,如果您只想验证表单的几个字段,您可以创建一个简单的验证为

    const required = value => (value ? undefined : 'Required')
    

    并将其添加到您的字段中

    <Field {...props}
        validate={required}
    </Field>
    

    【讨论】:

    • 我的意思是我有一个表单和两个按钮,两个按钮负责执行两个不同的任务,并且两个按钮都会在一些 i/o 调用之前进行一些基本的验证。单击“忘记”时,我必须进行验证,提交时,验证由 redux 表单本身负责。
    • 我不明白。您的验证是“redux-form 验证”吗?还是其他 Redux 海关操作验证?
    • 点击忘记我想做一些自定义验证
    猜你喜欢
    • 2019-03-27
    • 2017-09-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多