1 | class ModelProperty {
|
2 | constructor (model, key, display, validators, defaultValue) {
|
3 | this._model = model
|
4 | this._key = key
|
5 | this._display = display
|
6 | this._validators = validators
|
7 | this._defaultValue = defaultValue
|
8 | this.validation = {
|
9 | message: '',
|
10 | messages: [],
|
11 | valid: true,
|
12 | dirty: false
|
13 | }
|
14 | this.value = null
|
15 | }
|
16 |
|
17 | check () {
|
18 | return this.validate(this.value)
|
19 | }
|
20 |
|
21 | validate (value) {
|
22 | let validators = this._validators
|
23 | let messages = []
|
24 | let allValid = true
|
25 | if (validators != null && typeof validators !== 'undefined') {
|
26 | validators.forEach((v) => {
|
27 | let validatorResult = v.apply(this, value)
|
28 | if (!validatorResult.valid) {
|
29 | allValid = false
|
30 | messages.push(validatorResult.message)
|
31 | }
|
32 | })
|
33 | }
|
34 | this.validation = {
|
35 | valid: allValid,
|
36 | messages: messages,
|
37 | message: messages[0] || '',
|
38 | dirty: true
|
39 | }
|
40 | return this.validation
|
41 | }
|
42 |
|
43 | default () {
|
44 | this.value = this._defaultValue
|
45 | this.validation = {
|
46 | message: '',
|
47 | messages: [],
|
48 | valid: true,
|
49 | dirty: false
|
50 | }
|
51 | }
|
52 |
|
53 | defaultValidation () {
|
54 | this.validation = {
|
55 | message: '',
|
56 | messages: [],
|
57 | valid: true,
|
58 | dirty: false
|
59 | }
|
60 | }
|
61 |
|
62 | getValidation () {
|
63 | return this.validation
|
64 | }
|
65 |
|
66 | setValidation (validation) {
|
67 | this.validation = validation
|
68 | }
|
69 |
|
70 | getModel () {
|
71 | return this._model
|
72 | }
|
73 |
|
74 | getValue () {
|
75 | return this.value
|
76 | }
|
77 |
|
78 | setValue (value) {
|
79 | let validationResult = this.validate(value)
|
80 | this.value = value
|
81 | this.validation = validationResult
|
82 | this.dirty = true
|
83 | }
|
84 |
|
85 | set (value) {
|
86 | this.value = value
|
87 | }
|
88 |
|
89 | getDisplay () {
|
90 | return this._display
|
91 | }
|
92 |
|
93 | getKey () {
|
94 | return this._key
|
95 | }
|
96 | }
|
97 |
|
98 | export default ModelProperty
|