@@ -25,11 +25,6 @@ export abstract class BaseFormModel {
25
25
}
26
26
27
27
private static isFieldValid ( field ) : boolean {
28
- // console.log(field);
29
- // console.log('type checker');
30
- // console.log('isInput: ' + (field.constructor === InputFieldModel));
31
- // console.log('isForm: ' + (field instanceof BaseFormModel));
32
- // console.log('isArray: ' + (field instanceof Array));
33
28
if ( field instanceof InputFieldModel ||
34
29
field instanceof BaseFormModel ||
35
30
field instanceof Array ) {
@@ -39,22 +34,19 @@ export abstract class BaseFormModel {
39
34
return false ;
40
35
}
41
36
42
- // checks if one of the object's fields has an error
43
- // if so returns true
44
- // otherwise returns false
45
- hasError ( nonRequiredParam : Array < string > = [ ] ) : boolean {
46
- const keys = ( Object . keys ( this ) ) ;
37
+ private static modelHasError ( model : BaseFormModel , nonRequiredParam : Array < string > = [ ] ) : boolean {
38
+ const keys = ( Object . keys ( model ) ) ;
47
39
48
40
for ( const key of keys ) {
49
- const field = this [ key ] ;
41
+ const field = model [ key ] ;
50
42
51
43
if ( ! BaseFormModel . isFieldValid ( field ) ) {
52
44
throw new Error ( INVALID_FORM_MODEL ) ;
53
45
}
54
46
55
47
let hasError = false ;
56
48
if ( field instanceof BaseFormModel ) {
57
- hasError = field . hasError ( nonRequiredParam ) ;
49
+ hasError = this . modelHasError ( field , nonRequiredParam ) ;
58
50
} else if ( field instanceof Array ) {
59
51
hasError = this . arrayHasError ( field , nonRequiredParam ) ;
60
52
} else if ( field . errors . length > 0 ||
@@ -70,13 +62,12 @@ export abstract class BaseFormModel {
70
62
return false ;
71
63
}
72
64
73
- private arrayHasError ( fields : Array < any > , nonRequiredParam : Array < string > = [ ] ) : boolean {
74
- console . log ( 'checking if array has error' ) ;
65
+ private static arrayHasError ( fields : Array < any > , nonRequiredParam : Array < string > = [ ] ) : boolean {
75
66
for ( const field of fields ) {
76
67
let hasError = false ;
68
+
77
69
if ( field instanceof BaseFormModel ) {
78
- console . log ( 'BaseFormModel' ) ;
79
- hasError = field . arrayHasError ( nonRequiredParam ) ;
70
+ hasError = this . modelHasError ( field , nonRequiredParam ) ;
80
71
} else if ( field instanceof Array ) {
81
72
hasError = this . arrayHasError ( field , nonRequiredParam ) ;
82
73
} else if ( field . hasOwnProperty ( 'value' ) && field . hasOwnProperty ( 'errors' ) ) {
@@ -91,16 +82,8 @@ export abstract class BaseFormModel {
91
82
return false ;
92
83
}
93
84
94
- // returns an object ready to be sent to the backend
95
- // Ex:
96
- // {
97
- // 'property1': value1,
98
- // 'property2': value2,
99
- // 'property3': value3,
100
- // 'property4': value4
101
- // }
102
- prepare2send ( flat = false , propertiesIgnored : Array < string > = [ ] ) : PREPARE_TO_SEND_RETURN {
103
- const keys = ( Object . keys ( this ) ) ;
85
+ private static prepareModel2send ( model : BaseFormModel , flat = false , propertiesIgnored : Array < string > = [ ] ) : PREPARE_TO_SEND_RETURN {
86
+ const keys = ( Object . keys ( model ) ) ;
104
87
105
88
let ret = { } ;
106
89
@@ -109,14 +92,14 @@ export abstract class BaseFormModel {
109
92
continue ;
110
93
}
111
94
112
- const field = this [ key ] ;
95
+ const field = model [ key ] ;
113
96
114
97
if ( ! BaseFormModel . isFieldValid ( field ) ) {
115
98
throw new Error ( INVALID_FORM_MODEL ) ;
116
99
}
117
100
118
101
if ( field instanceof BaseFormModel ) {
119
- const returnedValue = field . prepare2send ( flat , propertiesIgnored ) ;
102
+ const returnedValue = this . prepareModel2send ( field , flat , propertiesIgnored ) ;
120
103
121
104
if ( flat ) {
122
105
// merge the objects
@@ -138,14 +121,14 @@ export abstract class BaseFormModel {
138
121
return ret ;
139
122
}
140
123
141
- private prepareArray2send ( fields : Array < VALID_TYPES > ,
142
- flat : boolean , propertiesIgnored : Array < string > ) :
143
- Array < VALID_TYPES > | null {
124
+ private static prepareArray2send ( fields : Array < VALID_TYPES > ,
125
+ flat : boolean , propertiesIgnored : Array < string > ) :
126
+ Array < VALID_TYPES > | null {
144
127
const array = [ ] ;
145
128
146
129
for ( const field of fields ) {
147
130
if ( field instanceof BaseFormModel ) {
148
- const returnedValue = field . prepare2send ( flat , propertiesIgnored ) ;
131
+ const returnedValue = this . prepareModel2send ( field , flat , propertiesIgnored ) ;
149
132
array . push ( returnedValue ) ;
150
133
} else if ( field instanceof Array ) {
151
134
const returnedValue = this . prepareArray2send ( field , flat , propertiesIgnored ) ;
@@ -162,6 +145,26 @@ export abstract class BaseFormModel {
162
145
return array ;
163
146
}
164
147
148
+ // checks if one of the object's fields has an error
149
+ // if so returns true
150
+ // otherwise returns false
151
+ hasError ( nonRequiredParam : Array < string > = [ ] ) : boolean {
152
+ return BaseFormModel . modelHasError ( this , nonRequiredParam ) ;
153
+ }
154
+
155
+
156
+ // returns an object ready to be sent to the backend
157
+ // Ex:
158
+ // {
159
+ // 'property1': value1,
160
+ // 'property2': value2,
161
+ // 'property3': value3,
162
+ // 'property4': value4
163
+ // }
164
+ prepare2send ( flat = false , propertiesIgnored : Array < string > = [ ] ) {
165
+ return BaseFormModel . prepareModel2send ( this , flat , propertiesIgnored )
166
+ }
167
+
165
168
// returns a list of the name of all properties that have input === ''
166
169
//
167
170
// this function returns a list instead of a boolean because it might be useful
0 commit comments