如何不以redux形式在validate函数中返回字符串

问题描述

我有一个带有输入字段和同步验证的表单。同步验证会产生错误信息,错误信息会显示在输入框下方。

但是,在某些情况下,我只希望输入字段具有红色边框而没有错误消息。我尝试使用字段级验证。

status_config.findOne({ where: created_time **need to implement here to get last added value})

然后我的字段看起来像这样:

import {validateDateString} from "some/path";

const renderField = ({input,value,Meta: {touched,asyncValidating,error,valid}}) => {
        return (
            <div className=`${validateDateString(input.value) ? '': 'border-error'}`>
                <input type="text" {...input}/>
                {touched && error && <div className="form-group__error">{error}</div>}
            </div>
        );
};

const validate = (dateString) => {
   if(validateDateString(dateString)) return undefined;
   return false; 
}

在字段级验证中,我不希望显示错误消息,只在同步验证中显示。因此,我不想在 <Field name={"myName"} component={renderField} validate={validate} /> 函数中返回字符串,因为该字符串将显示在输入字段下方。 docs 说它不必是字符串,所以我要返回 validate

如果值无效,验证函数应该返回一个 错误。这通常是一个字符串,但不一定是。

但似乎不起作用:表单仍在提交中。我在验证功能中做错了什么,还是有其他方法可以防止手动提交表单?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)