import{_ as s,c as a,a as p,o as e}from"./app-CDlLWk6N.js";const t={};function c(l,n){return e(),a("div",null,n[0]||(n[0]=[p(`

自定义校验

如果为常规的校验(如是否填写,常规正则校验),可以直接将校验规则提那家在表单生成的config.ts文件中相应字段的rules属性中。如果校验逻辑比较复杂,不想在config.ts文件中写,可以在拆分出的CustomDevForm.vue文件中的schemaMap中添加相应的校验。

computed: {
    // 这里需要增加一个计算属性 否则流程关联时字段读写状态会失效
    schemaMap() {
        const schemaMap = {};
        this.getSchemas.forEach((schema) => {
            // 在这里修改
            schemaMap[schema.key] = schema;
            if(schema.children) {
                schema.children.forEach(sChild=>{
                    if(sChild.list){
                        sChild.list.forEach(lChild=>{
                            // tab等结构的子元素在这里
                            schemaMap[lChild.key] = lChild;
                        });
                    } 
                });
            }
        });
        return schemaMap;
    }
},

可以使用remote远程校验,也可以使用regex校验函数。不过一般的form表单组件都自带对pattern的正则解析,所以一般不需要使用regex校验函数。具体rules的格式可查看ant-design-vue Form的文档或者APP端uni-app Form的文档。 这里只举一个远程校验的例子:

{
  username: [
    { 
      required: true, 
      message: '请输入用户名', 
      trigger: 'blur' 
    },
    {
      validator: async (rule, value) => {
        try {
          const { data } = await axios.get('/api/user/check', {
            params: { username: value }
          });
          if (data.exists) {
            return Promise.reject('该用户名已被使用');
          }
          return Promise.resolve();
        } catch (error) {
          return Promise.reject('验证服务不可用');
        }
      },
      trigger: 'blur'
    }
  ]
}
`,5)]))}const i=s(t,[["render",c]]),u=JSON.parse('{"path":"/front_dev/base/validation.html","title":"自定义校验","lang":"en-US","frontmatter":{},"headers":[],"git":{"updatedTime":1744257372000,"contributors":[{"name":"DESKTOP-45LLIKH\\\\11405","username":"","email":"1140589958@qq.com","commits":3}],"changelog":[{"hash":"51b826e36d1d26ef2ed843c669a1ab12b3fcb8f5","time":1744257372000,"email":"1140589958@qq.com","author":"DESKTOP-45LLIKH\\\\11405","message":"feat: 文档优化 新写 为工具栏添加按钮, 为表单新添加新字段"},{"hash":"c84155fcf9531f25802f735edad59713762e5ca9","time":1744251107000,"email":"1140589958@qq.com","author":"DESKTOP-45LLIKH\\\\11405","message":"feat: 已写文档优化"},{"hash":"bb33c9cfb0a16dc1df1c8e1434b41b052a489710","time":1744192947000,"email":"1140589958@qq.com","author":"DESKTOP-45LLIKH\\\\11405","message":"feat: 值计算,修改组件值,自定义校验,使用远程数据源"}]},"filePathRelative":"front_dev/base/validation.md"}');export{i as comp,u as data};