uni-app schema2code生成的表单校验规则存在bug

uni-app schema2code生成的表单校验规则存在bug

开发环境 版本号 项目创建方式
HbuilderX 3.4.7

操作步骤:

"name": {
    "bsonType": "string",
    "description": "商品名称",
    "title": "商品名称",
    "trim": "both",
    "maxLength": 20,
    "errorMessage": {
        "required": "{title}必填",
        "maxLength": "{title}不能大于{maxLength}个字符"
    }
}

用schema2code生成前端界面,不输入商品名称,点击提交

预期结果:

提示:商品名称必填

实际结果:

提示:{title}必填

bug描述:

如果,用schema2code生成前端界面后,不输入商品名称,点击提交时,校验表单,提示:{title}必填

个人猜测:可能是表结构中用了title字段,不用label字段。而schema2code还没兼容title字段


更多关于uni-app schema2code生成的表单校验规则存在bug的实战教程也可以访问 https://www.itying.com/category-93-b0.html

5 回复

title 标题,开发者维护时自用,详情 https://uniapp.dcloud.net.cn/uniCloud/schema.html#segment 不推荐在错误提示中使用,不对这个做兼容处理

更多关于uni-app schema2code生成的表单校验规则存在bug的实战教程也可以访问 https://www.itying.com/category-93-b0.html


还有另外一个问题,int类型,配置了maximum。自动生成的校验规则,怎么没提示 “{label}不能大于{maximum}” ?详情见本帖2楼

回复 全栈OkLin: 这种写法目前只能有一个生效,后续会改进,可以这样写minimum: {label}必须介于{minimum}and{maximum}之间

“remain_count”: { “bsonType”: “int”, “description”: “库存数量”, “label”: “库存数量”, “minimum”: 1, “maximum”: 999999, “errorMessage”: { “required”: “{label}必填”, “minimum”: “{label}不能小于{minimum}”, “maximum”: “{label}不能大于{maximum}” } },
自动生成的校验规则,怎么没提示 “{label}不能大于{maximum}” ?

在使用 uni-appschema2code 功能生成表单时,如果发现生成的校验规则存在 bug,可以按照以下步骤进行排查和修复:

1. 检查 Schema 定义

首先,确保你的 schema 定义是正确的。schema2code 会根据 schema 生成表单和校验规则,如果 schema 定义有误,生成的校验规则也可能有问题。

{
  "type": "object",
  "properties": {
    "username": {
      "type": "string",
      "minLength": 3,
      "maxLength": 20,
      "title": "用户名",
      "description": "请输入用户名"
    },
    "password": {
      "type": "string",
      "minLength": 6,
      "maxLength": 20,
      "title": "密码",
      "description": "请输入密码"
    }
  },
  "required": ["username", "password"]
}

2. 检查生成的校验规则

schema2code 会根据 schema 生成相应的校验规则。你可以检查生成的校验规则是否符合预期。

{
  username: {
    rules: [
      {
        required: true,
        errorMessage: '用户名不能为空'
      },
      {
        minLength: 3,
        maxLength: 20,
        errorMessage: '用户名长度应在3到20个字符之间'
      }
    ]
  },
  password: {
    rules: [
      {
        required: true,
        errorMessage: '密码不能为空'
      },
      {
        minLength: 6,
        maxLength: 20,
        errorMessage: '密码长度应在6到20个字符之间'
      }
    ]
  }
}

3. 手动修复校验规则

如果生成的校验规则存在问题,你可以手动修改生成的代码,确保校验规则符合你的需求。

{
  username: {
    rules: [
      {
        required: true,
        errorMessage: '用户名不能为空'
      },
      {
        minLength: 3,
        maxLength: 20,
        errorMessage: '用户名长度应在3到20个字符之间'
      },
      {
        pattern: /^[a-zA-Z0-9_]+$/,
        errorMessage: '用户名只能包含字母、数字和下划线'
      }
    ]
  },
  password: {
    rules: [
      {
        required: true,
        errorMessage: '密码不能为空'
      },
      {
        minLength: 6,
        maxLength: 20,
        errorMessage: '密码长度应在6到20个字符之间'
      }
    ]
  }
}

4. 测试表单校验

在修改完校验规则后,务必进行充分的测试,确保表单校验功能正常工作。

5. 反馈给官方

如果你确认是 schema2code 的 bug,可以将问题反馈给 uni-app 官方团队,提供详细的复现步骤和代码,以便他们修复问题。

6. 使用自定义校验规则

如果 schema2code 生成的校验规则无法满足你的需求,你可以完全自定义校验规则,而不依赖 schema2code 的自动生成功能。

export default {
  data() {
    return {
      formData: {
        username: '',
        password: ''
      },
      rules: {
        username: [
          { required: true, message: '用户名不能为空', trigger: 'blur' },
          { min: 3, max: 20, message: '用户名长度应在3到20个字符之间', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '密码不能为空', trigger: 'blur' },
          { min: 6, max: 20, message: '密码长度应在6到20个字符之间', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    submitForm() {
      this.$refs.form.validate(valid => {
        if (valid) {
          // 表单校验通过
        } else {
          // 表单校验失败
        }
      })
    }
  }
}
回到顶部