material-ui Date Picker 在 Redux Form 中将空值保存为 Date(0)
material-ui Date Picker saves empty value as Date(0) in Redux Form
我有一个 Redux 表单,它使用 material-ui 日期选择器来保存日期。如果有人不选择日期并提交表单,日期将保存为 1970-01-02。不幸的是,documentation 似乎没有包含任何处理它的道具,我无法通过验证解决它 - 这些字段必须是可选的。
是否有任何选项可以强制组件在未提供值时不输入 Date(0)?或者我应该使用另一个 DatePicker 工具?我们正在使用 material-ui v.0.19.4。我尝试过使用只会在更改时改变的空状态,但它没有帮助。
这就是 Fields 的样子。
<Field
name="endOfPartnership"
type="text"
component={DatePicker}
className={css.fullWidth}
floatingLabelFocusStyle={styles.floatingLabelColor}
underlineFocusStyle={styles.floatingLabelColor}
floatingLabelText={intl.formatMessage(defineMessages.endOfPartnership)}
fullWidth
formatDate={formatDate}
minDate={minDate}
/>
我找到了定义行为的地方,仅供参考。来自他们的源代码。
https://github.com/mui-org/material-ui/blob/v0.x/src/DatePicker/DatePicker.js
有这一行。
value={this.state.date ? formatDate(this.state.date)
其中formatDate
是,
formatDate = (date) => {
if (this.props.locale) {
const DateTimeFormat = this.props.DateTimeFormat || dateTimeFormat;
return new DateTimeFormat(this.props.locale, {
day: 'numeric',
month: 'numeric',
year: 'numeric',
}).format(date);
} else {
return formatIso(date);
}
};
我想这就是日期总是 "well formatted" 的原因。
将日期解析为字符串的函数似乎存在问题。
有人在日期转换方面遇到问题,并提出了一个非常丑陋的解决方法。我们切换到 moment.js,这让我们避免了之前的问题,我可以确认它现在可以在没有提供输入的情况下完美运行 => materialUI 日期选择器可以正常工作。
谢谢克里斯托弗!
我有一个 Redux 表单,它使用 material-ui 日期选择器来保存日期。如果有人不选择日期并提交表单,日期将保存为 1970-01-02。不幸的是,documentation 似乎没有包含任何处理它的道具,我无法通过验证解决它 - 这些字段必须是可选的。
是否有任何选项可以强制组件在未提供值时不输入 Date(0)?或者我应该使用另一个 DatePicker 工具?我们正在使用 material-ui v.0.19.4。我尝试过使用只会在更改时改变的空状态,但它没有帮助。
这就是 Fields 的样子。
<Field
name="endOfPartnership"
type="text"
component={DatePicker}
className={css.fullWidth}
floatingLabelFocusStyle={styles.floatingLabelColor}
underlineFocusStyle={styles.floatingLabelColor}
floatingLabelText={intl.formatMessage(defineMessages.endOfPartnership)}
fullWidth
formatDate={formatDate}
minDate={minDate}
/>
我找到了定义行为的地方,仅供参考。来自他们的源代码。
https://github.com/mui-org/material-ui/blob/v0.x/src/DatePicker/DatePicker.js
有这一行。
value={this.state.date ? formatDate(this.state.date)
其中formatDate
是,
formatDate = (date) => {
if (this.props.locale) {
const DateTimeFormat = this.props.DateTimeFormat || dateTimeFormat;
return new DateTimeFormat(this.props.locale, {
day: 'numeric',
month: 'numeric',
year: 'numeric',
}).format(date);
} else {
return formatIso(date);
}
};
我想这就是日期总是 "well formatted" 的原因。
将日期解析为字符串的函数似乎存在问题。
有人在日期转换方面遇到问题,并提出了一个非常丑陋的解决方法。我们切换到 moment.js,这让我们避免了之前的问题,我可以确认它现在可以在没有提供输入的情况下完美运行 => materialUI 日期选择器可以正常工作。
谢谢克里斯托弗!