在 Redux-Form 上,我可以在验证之后但在提交之前处理值吗?
On Redux-Form, can I process values after validation, but before they are submitted?
我正在使用 Redux-Form 并且遇到以下情况:
数字和日期时间很特殊,因为它们可能 无效 直到您完成字段填写(例如 12/08/
)。此外,您可能有多个字符串表示相同的值(例如 2.5000
和 2.5
)。
出于这个原因,我决定在表单和应用程序状态中将所有字段都保留为 strings。
问题是我需要在验证后处理字段的值,以便 JSON 进入服务器实际上有数字和日期时间,而不仅仅是字符串。
这是理想的解决方案吗?如果可以,我该怎么做?
你的推理似乎很有道理。你已经有了一个 "formatter" 运行 将你的数据转换成字符串,所以你只需要它的补充,一个 "parser",在 onSubmit
和 [=16= 之间] 打电话。
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>
)
}
我正在使用 Redux-Form 并且遇到以下情况:
数字和日期时间很特殊,因为它们可能 无效 直到您完成字段填写(例如 12/08/
)。此外,您可能有多个字符串表示相同的值(例如 2.5000
和 2.5
)。
出于这个原因,我决定在表单和应用程序状态中将所有字段都保留为 strings。
问题是我需要在验证后处理字段的值,以便 JSON 进入服务器实际上有数字和日期时间,而不仅仅是字符串。
这是理想的解决方案吗?如果可以,我该怎么做?
你的推理似乎很有道理。你已经有了一个 "formatter" 运行 将你的数据转换成字符串,所以你只需要它的补充,一个 "parser",在 onSubmit
和 [=16= 之间] 打电话。
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>
)
}