Skip to content

feat: ✨ Form 表单 validate 方法支持传入数组#829

Merged
Moonofweisheng merged 1 commit into
masterfrom
fix/form-validate
Jan 13, 2025
Merged

feat: ✨ Form 表单 validate 方法支持传入数组#829
Moonofweisheng merged 1 commit into
masterfrom
fix/form-validate

Conversation

@Moonofweisheng

@Moonofweisheng Moonofweisheng commented Jan 11, 2025

Copy link
Copy Markdown
Owner

Closes: #797

🤔 这个 PR 的性质是?(至少选择一个)

  • 日常 bug 修复
  • 新特性提交
  • 站点、文档改进
  • 演示代码改进
  • 组件样式/交互改进
  • TypeScript 定义更新
  • CI/CD 改进
  • 包体积优化
  • 性能优化
  • 功能增强
  • 国际化改进
  • 代码重构
  • 代码风格优化
  • 测试用例
  • 分支合并
  • 其他改动(是关于什么的改动?)

🔗 相关 Issue

#797

💡 需求背景和解决方案

form validate支持传入数组以校验多个指定字段,针对使用v-if隐藏的表单组件,不做校验。

☑️ 请求合并前的自查清单

⚠️ 请自检并全部勾选全部选项⚠️

  • 文档已补充或无须补充
  • 代码演示已提供或无须提供
  • TypeScript 定义已补充或无须补充

Summary by CodeRabbit

发行说明

  • 新功能

    • 增强表单验证功能,现支持同时验证多个字段
    • 表单组件新增动态显示/隐藏字段的验证能力
    • 新增“玛卡巴卡id”和“折扣”字段
  • 改进

    • 优化表单验证流程,提供更灵活的错误处理机制
    • 文档更新,详细说明表单组件的使用方法和验证规则
  • 示例更新

    • 新增表单示例,展示多种输入类型和验证场景
  • 文件上传

    • 更新文件上传组件的API端点

@vercel

vercel Bot commented Jan 11, 2025

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
wot-design-uni ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jan 13, 2025 3:49pm

@coderabbitai

coderabbitai Bot commented Jan 11, 2025

Copy link
Copy Markdown

概述

演练

此次更改主要关注表单组件的验证功能增强。文档和代码同步更新,支持对表单字段进行更灵活的验证。validate方法现在可以接受单个字段名或字段名数组,允许开发者同时验证多个特定字段。同时,相关的示例文件也进行了相应的更新,展示了新的验证方法和表单交互逻辑。

变更

文件 变更摘要
docs/component/form.md 更新表单组件文档,详细说明验证方法和表单行为
src/pages/form/demo2.vue 添加新的ID输入字段和验证逻辑
src/pages/form/demo3.vue 新增折扣字段,更新上传组件URL和验证规则
src/uni_modules/wot-design-uni/components/wd-form/types.ts 修改validate方法签名,支持字符串数组
src/uni_modules/wot-design-uni/components/wd-form/wd-form.vue 重构验证逻辑,支持多字段验证和错误处理

对应问题评估

目标 是否解决 说明
支持字符串数组验证 [#797]
允许部分表单字段验证 [#797]

可能相关的PR

诗歌

🐰 验证之舞,字段齐欢唱
数组飞舞,规则轻轻扬
表单如诗,错误不再慌
灵活验证,开发乐逍遥
兔子微笑,代码飞翔 🌈


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@netlify

netlify Bot commented Jan 11, 2025

Copy link
Copy Markdown

Deploy Preview for wot-design-uni ready!

Name Link
🔨 Latest commit 96c5889
🔍 Latest deploy log https://app.netlify.com/sites/wot-design-uni/deploys/678535d76a21000008e441f4
😎 Deploy Preview https://deploy-preview-829--wot-design-uni.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🧹 Nitpick comments (2)
src/uni_modules/wot-design-uni/components/wd-form/wd-form.vue (1)

149-156: 性能优化建议

通过过滤不存在对应子组件的规则来优化性能是个很好的改进。建议添加一个缓存机制来存储 childrenProps,避免在每次验证时重新计算。

建议实现方式:

+const cachedChildrenProps = ref<string[]>([])
+
+watch(children, () => {
+  cachedChildrenProps.value = children.map((child) => child.prop)
+})
+
 function getMergeRules() {
   const mergedRules: FormRules = deepClone(props.rules)
-  const childrenProps = children.map((child) => child.prop)
+  const childrenProps = cachedChildrenProps.value

   Object.keys(mergedRules).forEach((key) => {
     if (!childrenProps.includes(key)) {
       delete mergedRules[key]
     }
   })
docs/component/form.md (1)

489-492: 📝 建议补充v-if示例的注意事项

关于使用v-if隐藏组件不进行校验的说明,建议补充一些常见场景下的注意事项,比如动态表单场景。

建议添加以下内容:

在表单中,如果某个组件使用 `v-if` 隐藏,则不会对该组件进行校验。

+> 注意:在动态表单场景下,如果使用 `v-if` 控制表单项的显示与隐藏,需要注意表单验证规则的动态更新,以确保验证逻辑与表单项的显示状态保持一致。
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 834cd8e and 6283cf3.

📒 Files selected for processing (5)
  • docs/component/form.md (5 hunks)
  • src/pages/form/demo2.vue (5 hunks)
  • src/pages/form/demo3.vue (6 hunks)
  • src/uni_modules/wot-design-uni/components/wd-form/types.ts (2 hunks)
  • src/uni_modules/wot-design-uni/components/wd-form/wd-form.vue (6 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (4)
  • GitHub Check: Redirect rules - wot-design-uni
  • GitHub Check: Header rules - wot-design-uni
  • GitHub Check: Pages changed - wot-design-uni
  • GitHub Check: Cloudflare Pages
🔇 Additional comments (7)
src/uni_modules/wot-design-uni/components/wd-form/types.ts (1)

75-78: 类型定义更新正确!

validate 方法的参数类型从 string 扩展为 string | Array<string> 的改动非常合理,这样能够支持同时验证多个表单字段。返回类型保持不变,确保了向后兼容性。

src/pages/form/demo2.vue (1)

76-87: 验证函数实现正确!

handleValidate 函数很好地展示了新的多字段验证功能,包括:

  • 使用数组语法同时验证多个字段
  • 适当的错误处理
  • 清晰的成功提示
src/uni_modules/wot-design-uni/components/wd-form/wd-form.vue (1)

54-64: 验证逻辑优化得当!

代码改进:

  • 使用 isArray 进行类型检查
  • 通过 reduce 高效过滤规则
  • 保持了代码的可读性
src/pages/form/demo3.vue (1)

111-115: 注意上传 URL 的安全性

使用 mock API 进行文件上传可能存在安全风险。建议:

  1. 使用 HTTPS
  2. 添加适当的认证机制
  3. 实现文件类型和大小的验证
docs/component/form.md (3)

Line range hint 397-427: ✨ 新功能文档清晰完整

文档很好地解释了validate方法新增的数组参数支持,使用场景描述准确。


464-477: 💡 示例代码结构合理

示例代码展示了如何使用数组参数进行多字段校验,代码结构清晰,便于理解。


1021-1021: ✨ API文档更新准确

Events表格中的validate方法参数说明已正确更新,包含了数组参数支持的说明。

Comment thread src/pages/form/demo2.vue
Comment thread src/pages/form/demo3.vue
@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Jan 13, 2025

Copy link
Copy Markdown

Deploying wot-design-uni with  Cloudflare Pages  Cloudflare Pages

Latest commit: 96c5889
Status: ✅  Deploy successful!
Preview URL: https://b327e6ac.wot-design-uni.pages.dev
Branch Preview URL: https://fix-form-validate.wot-design-uni.pages.dev

View logs

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (3)
src/pages/form/demo2.vue (1)

76-87: 建议优化错误处理反馈

当前实现仅在控制台输出错误信息。建议添加用户反馈以提升用户体验。

建议修改为:

 function handleValidate() {
   form
     .value!.validate(['phoneNumber', 'id'])
     .then(({ valid }) => {
       if (valid) {
         showSuccess('校验通过')
       }
     })
     .catch((error) => {
-      console.log(error, 'error')
+      showToast({
+        msg: '校验失败:' + error.message,
+        type: 'error'
+      })
     })
 }
src/uni_modules/wot-design-uni/components/wd-form/wd-form.vue (1)

149-156: 建议添加注释说明过滤逻辑

这段重要的优化逻辑缺少必要的注释说明。

建议添加如下注释:

+ // 优化:过滤掉不存在对应子组件的规则,避免不必要的验证
  const childrenProps = children.map((child) => child.prop)

  // 过滤掉在 children 中不存在对应子组件的规则
  Object.keys(mergedRules).forEach((key) => {
    if (!childrenProps.includes(key)) {
      delete mergedRules[key]
    }
  })
src/pages/form/demo3.vue (1)

111-115: 建议优化上传URL配置

建议将上传URL配置为可配置项,而不是硬编码在组件中。

建议修改为:

 <wd-upload
   :file-list="model.fileList"
-  action="https://mockapi.eolink.com/zhTuw2P8c29bc981a741931bdd86eb04dc1e8fd64865cb5/upload"
+  :action="uploadConfig.url"
   @change="handleFileChange"
 ></wd-upload>

并在script中添加配置:

const uploadConfig = {
  url: import.meta.env.VITE_UPLOAD_URL || 'https://mockapi.eolink.com/zhTuw2P8c29bc981a741931bdd86eb04dc1e8fd64865cb5/upload'
}
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 6283cf3 and 96c5889.

📒 Files selected for processing (5)
  • docs/component/form.md (5 hunks)
  • src/pages/form/demo2.vue (5 hunks)
  • src/pages/form/demo3.vue (6 hunks)
  • src/uni_modules/wot-design-uni/components/wd-form/types.ts (2 hunks)
  • src/uni_modules/wot-design-uni/components/wd-form/wd-form.vue (6 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • src/uni_modules/wot-design-uni/components/wd-form/types.ts
⏰ Context from checks skipped due to timeout of 90000ms (4)
  • GitHub Check: Redirect rules - wot-design-uni
  • GitHub Check: Header rules - wot-design-uni
  • GitHub Check: Pages changed - wot-design-uni
  • GitHub Check: Cloudflare Pages
🔇 Additional comments (5)
src/pages/form/demo2.vue (2)

34-44: 代码实现规范且完整!

新增的输入字段实现完整,包含了必要的验证规则和事件处理。


49-50: 按钮实现清晰且功能明确!

新增的校验按钮很好地展示了表单组件新增的多字段校验功能。

src/uni_modules/wot-design-uni/components/wd-form/wd-form.vue (1)

54-64: 验证逻辑增强实现优秀!

支持数组参数的验证逻辑实现清晰,类型处理完善,兼容性好。

src/pages/form/demo3.vue (1)

91-99: 🛠️ Refactor suggestion

建议加强折扣输入验证规则

当前折扣字段的验证规则过于简单,建议添加数值范围验证。

建议修改验证规则:

 discount: [
   {
     required: true,
     message: '请输入优惠金额',
+    pattern: /^0\.[1-9]\d?$|^1\.0{1,2}$|^1$/,
+    message: '折扣必须是0-1之间的数值'
   }
 ]

Likely invalid or redundant comment.

docs/component/form.md (1)

Line range hint 397-492: 文档更新全面且清晰!

新增的多字段校验功能文档说明完整,示例代码清晰,对隐藏组件的校验行为说明准确。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[新功能需求] Form validate事件prop支持string[]

1 participant