验证绑定

avalon2新引入的指令,只能用于form元素上,用于为表单添加验证功能。它需要与ms-duplex, ms-rules指令一起配合使用。

此组件要依赖于Promise,显然Promise支持情况不太好,因此建议大家配合 es6 Promise库一起使用。

ms-validate  - 图1

ms-validate的值应该对应一个对象,由于对象比较大,建议写在vm,像下面那样:

  1. vm.validate = {
  2. onValidateAll: function(reasons){
  3. //返回一个数组,如果长度为零说明没有错
  4. },
  5. onError: avalon.noop,//针对单个表单元素(使用了ms-duplex的input, select)
  6. onSuccess: avalon.noop,//针对单个表单元素
  7. onComplete: avalon.noop,//针对单个表单元素
  8. onReset: avalon.noop,//针对单个表单元素
  9. validateInBlur: true, // {Boolean} true,在blur事件中进行验证,触发onSuccess, onError, onComplete回调
  10. validateInKeyup: true, // {Boolean} true,在keyup事件中进行验证,触发onSuccess, onError, onComplete回调
  11. validateAllInSubmit: true, // {Boolean} true,在submit事件中执行onValidateAll回调
  12. resetInFocus: true, // {Boolean} true,在focus事件中执行onReset回调,
  13. deduplicateInValidateAll: false // {Boolean} false,在validateAll回调中对reason数组根据元素节点进行去重
  14. }

在一般情况下validateInBlur, validateInKeyup,validateAllInSubmit,resetInFocus都是默认的就行了。

onError,onSuccess,onComplete, onValidateAll的第一个参数都是reasons对象,this指向被验证的元素,reason里面有你需要的各种东西.

  1. var reason = {
  2. element: elem,
  3. data: field.data,
  4. message: elem.getAttribute("data-" + ruleName + "-message") || elem.getAttribute("data-message") || hook.message,
  5. validateRule: ruleName,
  6. getMessage: getMessage
  7. }
  1. <body>
  2. <script>
  3. var vm = avalon.define({
  4. $id: "test",
  5. action: '',
  6. name: '',
  7. add: function(e) {
  8. e.preventDefault()
  9. this.action = "add.php";
  10. this.validate.onManual();
  11. },
  12. update: function(){
  13. this.action = "update.php";
  14. this.validate.onManual();
  15. },
  16. validate: {
  17. validateAllInSubmit: false,
  18. onSuccess: function(reasons, event) {
  19. console.log('成功',reasons)
  20. },
  21. onError: function(reasons, event) {
  22. console.log('有验证没有通过')
  23. },
  24. onValidateAll: function(reasons) {
  25. var form = this
  26. if(reasons.length) {
  27. // 表单有错误
  28. console.log("有验证没有通过",reasons);
  29. return false;
  30. } else {
  31. // 验证成功
  32. form.submit()
  33. }
  34. }
  35. }
  36. })
  37. </script>
  38. <div ms-controller="test">
  39. <form :validate="@validate" id="f1" :attr="{ action: @action }">
  40. <input type="text" placeholder="Insert your Name" :duplex="@name" :rules="{ required: true, number:true }" />
  41. <input type="submit" value="新建用户" :click="@add"/>
  42. <input type="submit" value="修改用户" :click="@update"/>
  43. </form>
  44. </div>
  45. </body>

一个页面有多个验证

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>TODO supply a title</title>
  5. <meta charset="UTF-8">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <script src='./dist/avalon.js'></script>
  8. </head>
  9. <body>
  10. <script>
  11. var vm = avalon.define({
  12. $id: "test",
  13. action: '',
  14. name: '',
  15. email: '',
  16. add: function(e) {
  17. e.preventDefault()
  18. this.action = "add.php";
  19. this.validate.onManual();
  20. },
  21. update: function(e) {
  22. e.preventDefault()
  23. this.action = "update.php";
  24. this.validate.onManual();
  25. },
  26. add2: function(e) {
  27. e.preventDefault()
  28. this.action = "add.php";
  29. this.validate2.onManual();
  30. },
  31. update2: function(e) {
  32. e.preventDefault()
  33. this.action = "update.php";
  34. this.validate2.onManual();
  35. },
  36. validate: {
  37. name: 'validate1',
  38. validateAllInSubmit: false,
  39. onSuccess: function(reasons, event) {
  40. console.log('成功', reasons)
  41. },
  42. onError: function(reasons, event) {
  43. console.log('第1个表单没有通过验证 ', reasons.map(function(e) {
  44. return e.getMessage()
  45. }).join(' '))
  46. },
  47. onValidateAll: function(reasons) {
  48. var form = this
  49. if (reasons.length) {
  50. // 表单有错误
  51. console.log("第1个表单没有通过验证 ", reasons.map(function(e) {
  52. return e.getMessage()
  53. }).join(' '));
  54. return false;
  55. } else {
  56. // 验证成功
  57. // console.log(form) //在chrome控制台console面板勾选preserve log
  58. form.submit() //这里会发生跳转
  59. }
  60. }
  61. },
  62. validate2: {
  63. name: 'validate2',
  64. validateAllInSubmit: false,
  65. onSuccess: function(reasons, event) {
  66. console.log('成功', reasons)
  67. },
  68. onError: function(reasons, event) {
  69. console.log('第二个表单没有通过验证!', reasons.map(function(e) {
  70. return e.getMessage()
  71. }).join(' '))
  72. },
  73. onValidateAll: function(reasons) {
  74. var form = this
  75. if (reasons.length) {
  76. // 表单有错误
  77. console.log("第二个表单没有通过验证!", reasons.map(function(e) {
  78. return e.getMessage()
  79. }).join(' '));
  80. return false;
  81. } else {
  82. // 验证成功
  83. //console.log(form) //在chrome控制台console面板勾选preserve log
  84. form.submit() //这里会发生跳转
  85. }
  86. }
  87. }
  88. })
  89. </script>
  90. <div ms-controller="test">
  91. <form :validate="@validate" id="f1" :attr="{ action: @action }">
  92. <input type="text" placeholder="input your name" :duplex="@name" :rules="{ required: true, number:true }" />
  93. <input type="submit" value="新建用户" :click="@add" />
  94. <input type="submit" value="修改用户" :click="@update" />
  95. </form>
  96. <form :validate="@validate2" id="f2" :attr="{ action: @action }">
  97. <input type="text" placeholder="input your email" :duplex="@email" :rules="{ required: true, email:true }" />
  98. <input type="submit" value="新建邮箱" :click="@add2" />
  99. <input type="submit" value="修改邮箱" :click="@update2" />
  100. </form>
  101. </div>
  102. </body>
  103. </html>

有关它的详细用法建议看ms-rules指令