我想同时使用对redux表单的提交和同步验证。同时使用它们中的一个可以工作,但是当我将它们一起使用时,它看起来像同步验证会覆盖提交验证的结果。Redux表单 - 在同一表单中同步并提交验证
export default class MyForm extends Component{
render() {
...
return (
...<form>
<Button onClick={handleSubmit(submit)}>Submit</Button>
</form>...
)
}
}
我提交验证如下所示:
const submit = (values, dispatch) => {
return new Promise((resolve, reject) =>
{
setTimeout(() => {
reject({ Password: 'Wrong password', _error: 'Login failed'})
console.log("rejected");
}, 1000) // simulate server latency
})}
现在,我想添加的同步验证藏汉,所以我加入validate
藏汉:
let myForm = reduxForm({
...
validate: validateForm
})(MyForm);
这看起来像以下(剥去缩短)
const validateForm = function(values){
let errors = {}
if(values.Password == "notAllowed"){
errors.Password = "this is not allowed"
}
console.log("validated sync", errors);
return errors;
}
现在当我进入密码箱东西,并点击提交,我可以看到类似以下内容:
- “验证同步”,...
- (点击提交)
- (后一秒):拒绝*
- (紧随其后):已验证的同步。
我可以看到来自同步验证的错误,但没有来自提交验证的错误(显然是因为它被上面的控制台输出覆盖)。有什么方法可以将这两者结合起来?我想要现场验证和服务器端验证。
“您提议的事情不会发生,因为如果同步验证通过,Redux表单将不会提交您的表单。“ - 你的意思是”如果同步验证失败。“@ erik-r? –
是的,如果同步验证失败,它不会调用你的提交函数 –
@ErikR。我遇到了同样的问题。这可能会导致一个通用的服务器错误,我想将其设置为_error字段。错误在提交后被覆盖,那么保留此错误的最佳策略是什么? – gcedo