我正在使用Redux-Form,并且有以下情况:
数字和日期时间是特殊的,因为在您填写完字段(例如)之前,它们可能是无效的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>
)
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句