关于 uniCloud 在 uni-app 中设置 minLength 和 maxLength 限制后的提示问题

关于 uniCloud 在 uni-app 中设置 minLength 和 maxLength 限制后的提示问题

操作步骤:

{
  "bsonType": "object",
  "required": ["age", "name"],
  "permission": {
    "read": true,
    "create": true,
    "update": true,
    "delete": true
  },
  "properties": {
    "_id": {
      "description": "ID,系统自动生成"
    },
    "name": {
      "title": "姓名",
      "description": "请输入姓名",
      "label": "名字",
      "bsonType": "string",
      "minLength": 2,
      "maxLength": 5,
      "errorMessage": {
        "required": "{label}必填",
        "minLength": "{label}不能小于{minLength}个字符",
        "maxLength": "{label}不能大于{maxLength}个字符"
      }
    },
    "age": {
      "title": "年龄",
      "description": "请输入年龄",
      "bsonType": "int"
    },
    "gender": {
      "bsonType": "int",
      "title": "性别",
      "defaultValue": 0,
      "enum": [
        {
          "text": "保密",
          "value": 0
        },
        {
          "text": "男",
          "value": 1
        },
        {
          "text": "女",
          "value": 2
        }
      ]
    },
    "createTime": {
      "title": "时间",
      "bsonType": "timestamp",
      "defaultValue": {
        "$env": "now"
      }
    },
    "ip": {
      "title": "IP地址",
      "bsonType": "string",
      "forceDefaultValue": {
        "$env": "clientIP"
      }
    }
  }
}

预期结果:

名字填写超过5个字符应该显示名字不能大于5个字符

实际结果:

名字填写超过5个字符后,页面showLoading 弹出的提示为“名字不能小于2个字符”

bug描述:

{
  "name": {
    "title": "姓名",
    "description": "请输入姓名",
    "label": "名字",
    "bsonType": "string",
    "minLength": 2,
    "maxLength": 5,
    "errorMessage": {
      "required": "{label}必填",
      "minLength": "{label}不能小于{minLength}个字符",
      "maxLength": "{label}不能大于{maxLength}个字符"
    }
  }
}

这样写结构,在页面填写了6个字符后,还是会显示提示“不能小于2个字符”


更多关于关于 uniCloud 在 uni-app 中设置 minLength 和 maxLength 限制后的提示问题的实战教程也可以访问 https://www.itying.com/category-93-b0.html

2 回复

你上传了吗?

更多关于关于 uniCloud 在 uni-app 中设置 minLength 和 maxLength 限制后的提示问题的实战教程也可以访问 https://www.itying.com/category-93-b0.html


这是一个uniCloud数据库schema验证的常见问题。问题出在验证顺序上,uniCloud会按照字段定义的顺序进行验证,而不是按逻辑优先级。

在你的schema中,minLength验证先于maxLength执行。当输入超过5个字符时,系统首先检查minLength,发现字符数大于2,通过验证;然后检查maxLength,发现超过5个字符,应该触发maxLength的错误提示。

但实际表现显示的是minLength错误,这表明可能存在验证逻辑的bug。建议尝试以下解决方案:

  1. 调整验证顺序:将maxLength验证放在minLength之前
  2. 使用pattern验证:用正则表达式替代长度限制
    "pattern": "^.{2,5}$",
    "errorMessage": "{label}长度必须在2-5个字符之间"
回到顶部