⏱️ fix(token): disallow selecting expiration date earlier than now

Add custom validator to the `expired_time` DatePicker in `EditToken.js`
to ensure that selected expiration timestamps are strictly in the future.

- Introduce Promise-based validator that:
  • Rejects invalid date formats with a clear error message
  • Prevents past or current dates and shows “Expiration time cannot be earlier than the current time!”
- Keeps support for the special “never expires” value (-1)
- Blocks form submission until a valid future datetime is provided

This change prevents accidental creation of already expired tokens and
improves overall robustness of token management.
This commit is contained in:
t0ng7u
2025-07-12 03:42:05 +08:00
parent 7c4b83a430
commit 0a434d3b3a
2 changed files with 18 additions and 1 deletions

View File

@@ -345,7 +345,23 @@ const EditToken = (props) => {
label={t('过期时间')}
type='dateTime'
placeholder={t('请选择过期时间')}
rules={[{ required: true, message: t('请选择过期时间') }]}
rules={[
{ required: true, message: t('请选择过期时间') },
{
validator: (rule, value) => {
// 允许 -1 表示永不过期,也允许空值在必填校验时被拦截
if (value === -1 || !value) return Promise.resolve();
const time = Date.parse(value);
if (isNaN(time)) {
return Promise.reject(t('过期时间格式错误!'));
}
if (time <= Date.now()) {
return Promise.reject(t('过期时间不能早于当前时间!'));
}
return Promise.resolve();
},
},
]}
showClear
style={{ width: '100%' }}
/>