-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Open
Labels
⭕️ bugSomething isn't workingSomething isn't working
Description
提问前先看看:
https://github.com/ryanhanwu/How-To-Ask-Questions-The-Smart-Way/blob/main/README-zh_CN.md
🐛 bug 描述
當 ProTable 設置 form.ignoreRules = false 時,表單驗證在以下兩種情況被繞過:
- 重置按鈕繞過驗證:點擊「重置」按鈕後,即使必填欄位為空,仍會發起請求
- 初始載入繞過驗證:頁面首次載入時,即使必填欄位為空,仍會發起初始請求
這導致 ignoreRules: false 的設置形同虛設,無法實現「必填欄位有值才發起請求」的業務需求。
不確定這是需求還是 BUG,這邊先以 BUG 提出。
📷 复现步骤
問題 1:重置按鈕繞過驗證
- 設置 ProTable 的
form={{ ignoreRules: false }} - 定義一個帶有
required規則的搜索欄位 - 在欄位中輸入值,然後點擊「查詢」→ 正常發起請求
- 清空欄位,點擊「查詢」→ 顯示驗證錯誤 ✅
- 輸入值後,點擊「重置」→ 問題:仍然發起請求,未驗證 ❌
問題 2:初始載入繞過驗證
- 設置 ProTable 的
form={{ ignoreRules: false }} - 定義一個帶有
required規則的搜索欄位(無 initialValue) - 頁面載入 → 問題:必填欄位為空仍發起初始請求 ❌
🏞 期望结果
- 重置按鈕:當
ignoreRules: false時,重置後應該先驗證表單,驗證失敗則不發起請求 - 初始載入:當
ignoreRules: false時,初始載入也應該驗證表單,驗證失敗則不發起初始請求
💻 复现代码
問題 1:重置繞過驗證
https://codesandbox.io/p/sandbox/github-issue-protable-chong-zhi-rao-guo-yan-zheng-s7sz7w
問題 2:初始載入繞過驗證
https://codesandbox.io/p/devbox/github-issue-protable-chong-zhi-rao-guo-yan-zheng-forked-mxvgy4
© 版本信息
- ProComponents 版本: ^2.8.10
- umi 版本: N/A
- 浏览器环境: Chrome
- 开发环境: Windows(WSL)
🚑 其他信息
📝 預期改動
文件 1:src/table/components/Form/index.tsx
onReset 方法(約第 70-109 行):
onReset = async (value: Partial<U>) => {
const { form, formRef, /* ... */ } = this.props;
// 根據 ignoreRules 決定是否驗證
if (form?.ignoreRules === false) {
try {
await formRef?.current?.validateFields();
} catch {
action.current?.setPageInfo?.({ current: 1 });
onReset?.();
return; // 驗證失敗,不發起請求
}
}
// ... 原有邏輯
};文件 2:src/table/components/Form/FormRender.tsx
onInit 回調(約第 229-248 行)需新增驗證邏輯:
onInit={async (values: T, form) => {
formRef.current = form;
if (type !== 'form') {
// ... 原有 pageInfo 設置邏輯
if (manualRequest) return;
// 检查 ignoreRules 设置,如果为 false,则需验证表单
const ignoreRules = getFormConfigs(isForm, formConfig || {})?.ignoreRules;
if (ignoreRules === false) {
try {
await form.validateFields();
} catch {
return; // 驗證失敗,不發起初始請求
}
}
submit(values, true);
}
}}✅ 預期測試項
| # | 測試場景 | 操作步驟 | 預期結果 |
|---|---|---|---|
| 1 | 重置 + ignoreRules=false + 驗證通過 | 填寫必填欄位 → 點擊重置 | 發起請求 ✅ |
| 2 | 重置 + ignoreRules=false + 驗證失敗 | 必填欄位為空 → 點擊重置 | 不發起請求,顯示驗證錯誤 |
| 3 | 重置 + ignoreRules=true(預設) | 必填欄位為空 → 點擊重置 | 發起請求(向下兼容) |
| 4 | 初始載入 + ignoreRules=false + 必填項有初始值 | 頁面載入 | 發起請求 ✅ |
| 5 | 初始載入 + ignoreRules=false + 必填項無初始值 | 頁面載入 | 不發起請求 |
| 6 | 初始載入 + ignoreRules=true(預設) | 頁面載入 | 發起請求(向下兼容) |
| 7 | 初始載入 + manualRequest=true | 頁面載入 | 不發起請求(原有行為不變) |
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
⭕️ bugSomething isn't workingSomething isn't working