我可以在隐藏的输入字段上使用带有 redux-form 的异步模糊验证吗? [英] Can I use Asynchronous Blur Validation with redux-form on a hidden input field?

查看:31
本文介绍了我可以在隐藏的输入字段上使用带有 redux-form 的异步模糊验证吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 redux-form 特定问题.

I have a redux-form specific question.

我的表单中有一个隐藏的输入:

I have a hidden input in my form:

<input type="hidden" {...field} />

并希望通过向服务器发出请求来执行一些异步验证.实现这一目标的最优雅/最合适的方法是什么?我一直在研究异步模糊验证,这似乎是正确的,除了隐藏输入似乎无法触发模糊事件.

And would like to perform some asynchronous validation by making a request to the server. What is the most elegant/appropriate way to achieve this? I've been looking into Asynchronous Blur Validation, which seems like the right thing except for the fact that a hidden input doesn't seem to be able to trigger a blur event.

编辑

通过将以下中间件代码连接到 Redux,我设法得到了我想要的:

I managed to get what I wanted by hooking up the following middleware code to Redux:

  if (action.type === ActionTypes.MY_EVENT) {
    MyModule.asyncValidate({
      myField: store.getState().form.myForm.myField.value,
    }).catch((error) => {
      store.dispatch(stopAsyncValidation('myForm', error));
    });
  }

推荐答案

是的,当隐藏的输入模糊时询问如何做某事就像当你的水干了时询问如何做某事.

Yeah, asking how to do something when a hidden input blurs is like asking how to do something when your water is dry.

隐藏输入的概念在 redux-form 中并不真正存在,因为隐藏输入是基于旧的 HTML 提交表单的方式.redux-form 中的隐藏输入"只是出现在 fields 列表中的一个字段(因此它将被提交),但不会呈现给任何输入.我对主键执行此操作,其中我的 fields 列表是 ['id', 'name', 'email'],并且我只呈现 name 的输入email,但是这三个都被提交了.

The concept of hidden inputs doesn't really exist in redux-form, as hidden inputs are based on the ye olde HTML way of submitting forms. A "hidden input" in redux-form is just a field that appears in the fields list (so it will be submitted), but that is not rendered to any input. I do this for primary keys, where my fields list is ['id', 'name', 'email'], and I only render inputs for name and email, but all three get submitted.

我认为您想要的是在渲染(非隐藏)字段模糊时触发异步验证,并将所有字段,甚至未渲染的字段传递给异步验证函数.在上面的例子中,这样的事情会起作用:

I think what you want is for your async validation to fire when a rendered (non-hidden) field is blurred, and for all of the fields, even the ones that are not rendered, to be passed to the async validation function. With the example above, something like this would work:

reduxForm({
  form: 'myForm',
  fields: ['id', 'name', 'email'],
  asyncValidate: doServerSideAsyncValidation,
  asyncBlurFields: ['name', 'email'] // both rendered fields will trigger async validation
})

这篇关于我可以在隐藏的输入字段上使用带有 redux-form 的异步模糊验证吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆