Javascript 在Redux表单上,我可以在验证之后但在提交之前处理值吗?

Javascript 在Redux表单上,我可以在验证之后但在提交之前处理值吗?,javascript,reactjs,redux,redux-form,Javascript,Reactjs,Redux,Redux Form,我使用的是以下场景: 数字和日期时间是特殊的,因为在您填写完字段(例如12/08/)之前,它们可能是无效的。此外,您可能有多个表示相同值的字符串(例如2.5000和2.5) 出于这个原因,我决定在表单和应用程序状态下将所有字段都保留为字符串 问题是,在验证之后,我需要处理字段的值,以便发送到服务器的JSON实际上有数字和日期时间,而不仅仅是字符串 这是理想的解决方案吗?如果是这样,我该怎么做呢?你的推理似乎很合理。您已经运行了一个“格式化程序”来将数据转换成字符串,因此您只需要在表单onSubm

我使用的是以下场景:

数字和日期时间是特殊的,因为在您填写完字段(例如
12/08/
)之前,它们可能是无效的。此外,您可能有多个表示相同值的字符串(例如
2.5000
2.5

出于这个原因,我决定在表单和应用程序状态下将所有字段都保留为字符串

问题是,在验证之后,我需要处理字段的值,以便发送到服务器的JSON实际上有数字和日期时间,而不仅仅是字符串


这是理想的解决方案吗?如果是这样,我该怎么做呢?

你的推理似乎很合理。您已经运行了一个“格式化程序”来将数据转换成字符串,因此您只需要在表单
onSubmit
和ajax调用之间添加一个“解析器”

render() {
  const { fields, handleSubmit } = this.props
  return (
    <form onSubmit={handleSubmit(values => {
        // all values are guaranteed to pass sync validation here,
        // so they should all parse just fine.
        const parsedValues = parseIntoRealDataTypes(values)
        return ajax.post('/api/myWidgets', parsedValues)
          .then(response => {
            // rejoice
          })
      })}>
    </form>
  )
}
render(){
const{fields,handleSubmit}=this.props
返回(
{
//此处保证所有值都通过同步验证,
//所以他们应该都很好。
const parsedValues=parseIntoRealDataTypes(值)
返回ajax.post('/api/myWidgets',parsedValue)
。然后(响应=>{
//高兴
})
})}>
)
}

你的推理似乎很合理。您已经运行了一个“格式化程序”来将数据转换成字符串,因此您只需要在表单
onSubmit
和ajax调用之间添加一个“解析器”

render() {
  const { fields, handleSubmit } = this.props
  return (
    <form onSubmit={handleSubmit(values => {
        // all values are guaranteed to pass sync validation here,
        // so they should all parse just fine.
        const parsedValues = parseIntoRealDataTypes(values)
        return ajax.post('/api/myWidgets', parsedValues)
          .then(response => {
            // rejoice
          })
      })}>
    </form>
  )
}
render(){
const{fields,handleSubmit}=this.props
返回(
{
//此处保证所有值都通过同步验证,
//所以他们应该都很好。
const parsedValues=parseIntoRealDataTypes(值)
返回ajax.post('/api/myWidgets',parsedValue)
。然后(响应=>{
//高兴
})
})}>
)
}