回答編集履歴
1
コメントを受けて、ループでextendするように追記
answer
CHANGED
@@ -19,4 +19,58 @@
|
|
19
19
|
}
|
20
20
|
extend('exists', exists)
|
21
21
|
}
|
22
|
+
```
|
23
|
+
|
24
|
+
|
25
|
+
|
26
|
+
※追記
|
27
|
+
|
28
|
+
forループで登録したいようなので、このようにしたら動くかもしれません。
|
29
|
+
|
30
|
+
```js
|
31
|
+
// plugins/vee-validate.js
|
32
|
+
import Vue from 'vue'
|
33
|
+
import { extend, localize, ValidationObserver, ValidationProvider } from 'vee-validate' // 使用する機能
|
34
|
+
import ja from 'vee-validate/dist/locale/ja.json' // エラーメッセージの日本語化用
|
35
|
+
import * as rules from 'vee-validate/dist/rules' // 全てのバリデーションルール
|
36
|
+
import * as myRules from '@/rules' // 全てのカスタムバリデーションルール
|
37
|
+
|
38
|
+
// forループで全てのバリデーションルールをextendで登録する
|
39
|
+
for (const key in rules) {
|
40
|
+
// eslint-disable-next-line import/namespace
|
41
|
+
extend(key, rules[key])
|
42
|
+
}
|
43
|
+
|
44
|
+
Vue.component('ValidationProvider', ValidationProvider)
|
45
|
+
Vue.component('ValidationObserver', ValidationObserver)
|
46
|
+
localize('ja', ja)
|
47
|
+
|
48
|
+
export default ctx => {
|
49
|
+
for (const key in myRules) {
|
50
|
+
// eslint-disable-next-line import/namespace
|
51
|
+
extend(key, myRules[key](ctx))
|
52
|
+
}
|
53
|
+
}
|
54
|
+
```
|
55
|
+
|
56
|
+
```js
|
57
|
+
// rules.js
|
58
|
+
const exists = ({ $axios }) => ({
|
59
|
+
params: ['table', 'column'],
|
60
|
+
message: '指定された{_field_}は存在しません。',
|
61
|
+
async validate (value, { table, column }) {
|
62
|
+
// todo::paramsの精査。500を返したい。
|
63
|
+
const params = {
|
64
|
+
value,
|
65
|
+
table,
|
66
|
+
column
|
67
|
+
}
|
68
|
+
const response = await $axios.get('/rules/exists', params)
|
69
|
+
return response.data
|
70
|
+
}
|
71
|
+
})
|
72
|
+
|
73
|
+
export { exists }
|
74
|
+
// 増えていく場合はこう
|
75
|
+
// export { exists, hoge, fuga }
|
22
76
|
```
|