Advertisement
Guest User

Untitled

a guest
May 16th, 2016
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.76 KB | None | 0 0
  1. # バリデーションタイミング変更
  2.  
  3. vue-validator は `validator` エレメントディレクティブと `v-validate` ディレクティブで自動的にバリデートを実行します。しかしながら時々、自動バリデーションを無効化し手動でバリデートを実行したい時があります。
  4.  
  5. ## `initial`
  6. vue-validator は初回コンパイルを終えると、それぞれの `v-validate` ディレクティブは自動的に対象エレメントのバリデートを実行します。もしこの挙動を望まない場合は、 `initial` 属性又は `v-validate` を指定できます:
  7.  
  8. ```html
  9. <div id="app">
  10. <validator name="validation1">
  11. <form novalidate>
  12. <div class="username-field">
  13. <label for="username">username:</label>
  14. <!-- 'inital' attribute is applied the all validators of target element (e.g. required, exist) -->
  15. <input id="username" type="text" initial="off" v-validate:username="['required', 'exist']">
  16. </div>
  17. <div class="password-field">
  18. <label for="password">password:</label>
  19. <!-- 'initial' optional is applied with `v-validate` validator (e.g. required only) -->
  20. <input id="password" type="password" v-validate:passowrd="{ required: { rule: true, initial: 'off' }, minlength: 8 }">
  21. </div>
  22. <input type="submit" value="send" v-if="$validation1.valid">
  23. </form>
  24. </validator>
  25. </div>
  26. ```
  27.  
  28. This is useful, when you need to suppress the validation (like the server-side validation) with async validation feature (explain later).
  29. これは非同期的な特徴のバリデーション(サーバーサイドバリデーションのような)を抑制する必要がある場合に便利です。(後ほど説明します)
  30.  
  31. ## `detect-blur` と `detect-change`
  32. vue-validator はフォーム要素(input, checkbox, select, 等)のDOMイベント (`input`, `blur`, `change`)を検知したら自動的にバリデートを実行します。このような場合は、`detect-change`, `detect-blur` 属性を使ってください:
  33.  
  34. ```html
  35. <div id="app">
  36. <validator name="validation">
  37. <form novalidate @submit="onSubmit">
  38. <h1>user registration</h1>
  39. <div class="username">
  40. <label for="username">username:</label>
  41. <input id="username" type="text"
  42. detect-change="off" detect-blur="off" v-validate:username="{
  43. required: { rule: true, message: 'required you name !!' }
  44. }" />
  45. </div>
  46. <div class="password">
  47. <label for="password">password:</label>
  48. <input id="password" type="password" v-model="password"
  49. detect-change="off" detect-blur="off" v-validate:password="{
  50. required: { rule: true, message: 'required you new password !!' },
  51. minlength: { rule: 8, message: 'your new password short too !!' }
  52. }" />
  53. </div>
  54. <div class="confirm">
  55. <label for="confirm">confirm password:</label>
  56. <input id="confirm" type="password"
  57. detect-change="off" detect-blur="off" v-validate:confirm="{
  58. required: { rule: true, message: 'required you confirm password !!' },
  59. confirm: { rule: password, message: 'your confirm password incorrect !!' }
  60. }" />
  61. </div>
  62. <div class="errors" v-if="$validation.touched">
  63. <validator-errors :validation="$validation"></validator-errors>
  64. </div>
  65. <input type="submit" value="register" />
  66. </form>
  67. </validator>
  68. </div>
  69. ```
  70.  
  71. ```javascript
  72. new Vue({
  73. el: '#app',
  74. data: {
  75. password: ''
  76. },
  77. validators: {
  78. confirm: function (val, target) {
  79. return val === target
  80. }
  81. },
  82. methods: {
  83. onSubmit: function (e) {
  84. // validate manually
  85. this.$validate(true)
  86.  
  87. if (this.$validation.invalid) {
  88. e.preventDefault()
  89. }
  90. }
  91. }
  92. })
  93. ```
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement