x-validator 表单校验

  1. const schema = {
  2. type: 'object',
  3. properties: {
  4. username: {
  5. 'type': 'string',
  6. 'title': '手机号',
  7. 'x-validator': [
  8. {
  9. required: true,
  10. message: '请输入用户手机号',
  11. },
  12. {
  13. pattern: /^[0-9]\d*$/g,
  14. message: '手机号格式错误'
  15. }
  16. ],
  17. 'x-decorator': 'FormItem',
  18. 'x-component': 'Input',
  19. 'x-component-props': {
  20. placeholder: '请输入用户手机号',
  21. },
  22. },
  23. }
  24. }

自定义函数验证

format 可选择的默认提供的正则

  • ‘url’,’email’,’ipv6’, ‘ipv4’,’idcard’,’taodomain’
  • ‘qq’,’phone’,’money’, ‘zh’, ‘date’,’zip’ ```javascript import { registerValidateRules } from ‘@formily/core’

registerValidateRules({ custom_format: /^\d+$/, // 只能是正则表达式 custom(value) { return value > 100 ? ‘error’ : ‘’ }, }) ```