9
9
i.icon
10
10
.helpText(v-html='field.help')
11
11
.field-wrap
12
- component(:is='getFieldType(field)', :disabled='fieldDisabled(field)', :model='model', :schema.sync='field', @model-updated='modelUpdated')
12
+ component(:is='getFieldType(field)', :disabled='fieldDisabled(field)', :model='model', :schema.sync='field', @model-updated='modelUpdated', @validated="onFieldValidated" )
13
13
.buttons(v-if='buttonVisibility(field)')
14
14
button(v-for='btn in field.buttons', @click='btn.onclick(model, field)', :class='btn.classes') {{ btn.label }}
15
15
.hint(v-if='field.hint') {{ field.hint }}
16
- .errors(v-if='errorsVisibility (field)')
17
- span(v-for='(error, index) in field.errors ', track-by='index') {{ error }}
16
+ .errors(v-if='fieldErrors (field).length > 0 ')
17
+ span(v-for='(error, index) in fieldErrors( field) ', track-by='index') {{ error }}
18
18
</template >
19
19
20
20
<script >
126
126
// Get style classes of field
127
127
getFieldRowClasses (field ) {
128
128
let baseClasses = {
129
- error: field . errors && field . errors .length > 0 ,
129
+ error: this . fieldErrors ( field) .length > 0 ,
130
130
disabled: this .fieldDisabled (field),
131
131
readonly: this .fieldReadonly (field),
132
132
featured: this .fieldFeatured (field),
153
153
// Get disabled attr of field
154
154
fieldDisabled (field ) {
155
155
if (isFunction (field .disabled ))
156
- return field .disabled (this .model );
156
+ return field .disabled . call (this , this .model , field, this );
157
157
158
158
if (isNil (field .disabled ))
159
159
return false ;
164
164
// Get required prop of field
165
165
fieldRequired (field ) {
166
166
if (isFunction (field .required ))
167
- return field .required (this .model );
167
+ return field .required . call (this , this .model , field, this );
168
168
169
169
if (isNil (field .required ))
170
170
return false ;
175
175
// Get visible prop of field
176
176
fieldVisible (field ) {
177
177
if (isFunction (field .visible ))
178
- return field .visible (this .model );
178
+ return field .visible . call (this , this .model , field, this );
179
179
180
180
if (isNil (field .visible ))
181
181
return true ;
186
186
// Get readonly prop of field
187
187
fieldReadonly (field ) {
188
188
if (isFunction (field .readonly ))
189
- return field .readonly (this .model );
189
+ return field .readonly . call (this , this .model , field, this );
190
190
191
191
if (isNil (field .readonly ))
192
192
return false ;
@@ -197,23 +197,42 @@ div
197
197
// Get featured prop of field
198
198
fieldFeatured (field ) {
199
199
if (isFunction (field .featured ))
200
- return field .featured (this .model );
200
+ return field .featured . call (this , this .model , field, this );
201
201
202
202
if (isNil (field .featured ))
203
203
return false ;
204
204
205
205
return field .featured ;
206
206
},
207
207
208
+ // Child field executed validation
209
+ onFieldValidated (res , errors , field ) {
210
+ // Remove old errors for this field
211
+ this .errors = this .errors .filter (e => e .field != field .schema );
212
+
213
+ if (! res && errors && errors .length > 0 ) {
214
+ // Add errors with this field
215
+ errors .forEach ((err ) => {
216
+ this .errors .push ({
217
+ field: field .schema ,
218
+ error: err
219
+ });
220
+ });
221
+ }
222
+
223
+ let isValid = this .errors .length == 0 ;
224
+ this .$emit (" validated" , isValid, this .errors );
225
+ },
226
+
208
227
// Validating the model properties
209
228
validate () {
210
229
this .clearValidationErrors ();
211
230
212
- each ( this .$children , (child ) => {
231
+ this .$children . forEach ( (child ) => {
213
232
if (isFunction (child .validate ))
214
233
{
215
- let err = child .validate ();
216
- each (err, (err ) => {
234
+ let errors = child .validate (true );
235
+ errors . forEach ( (err ) => {
217
236
this .errors .push ({
218
237
field: child .schema ,
219
238
error: err
222
241
}
223
242
});
224
243
225
- return this .errors .length == 0 ;
244
+ let isValid = this .errors .length == 0 ;
245
+ this .$emit (" validated" , isValid, this .errors );
246
+ return isValid;
226
247
},
227
248
228
249
// Clear validation errors
242
263
return field .buttons && field .buttons .length > 0 ;
243
264
},
244
265
245
- errorsVisibility (field ) {
246
- return field .errors && field .errors .length > 0 ;
266
+ fieldErrors (field ) {
267
+ let res = this .errors .filter (e => e .field == field);
268
+ return res .map (item => item .error );
247
269
}
248
270
}
249
271
};
0 commit comments