99 i.icon
1010 .helpText(v-html='field.help')
1111 .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" )
1313 .buttons(v-if='buttonVisibility(field)')
1414 button(v-for='btn in field.buttons', @click='btn.onclick(model, field)', :class='btn.classes') {{ btn.label }}
1515 .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 }}
1818</template >
1919
2020<script >
126126 // Get style classes of field
127127 getFieldRowClasses (field ) {
128128 let baseClasses = {
129- error: field . errors && field . errors .length > 0 ,
129+ error: this . fieldErrors ( field) .length > 0 ,
130130 disabled: this .fieldDisabled (field),
131131 readonly: this .fieldReadonly (field),
132132 featured: this .fieldFeatured (field),
153153 // Get disabled attr of field
154154 fieldDisabled (field ) {
155155 if (isFunction (field .disabled ))
156- return field .disabled (this .model );
156+ return field .disabled . call (this , this .model , field, this );
157157
158158 if (isNil (field .disabled ))
159159 return false ;
164164 // Get required prop of field
165165 fieldRequired (field ) {
166166 if (isFunction (field .required ))
167- return field .required (this .model );
167+ return field .required . call (this , this .model , field, this );
168168
169169 if (isNil (field .required ))
170170 return false ;
175175 // Get visible prop of field
176176 fieldVisible (field ) {
177177 if (isFunction (field .visible ))
178- return field .visible (this .model );
178+ return field .visible . call (this , this .model , field, this );
179179
180180 if (isNil (field .visible ))
181181 return true ;
186186 // Get readonly prop of field
187187 fieldReadonly (field ) {
188188 if (isFunction (field .readonly ))
189- return field .readonly (this .model );
189+ return field .readonly . call (this , this .model , field, this );
190190
191191 if (isNil (field .readonly ))
192192 return false ;
@@ -197,23 +197,42 @@ div
197197 // Get featured prop of field
198198 fieldFeatured (field ) {
199199 if (isFunction (field .featured ))
200- return field .featured (this .model );
200+ return field .featured . call (this , this .model , field, this );
201201
202202 if (isNil (field .featured ))
203203 return false ;
204204
205205 return field .featured ;
206206 },
207207
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+
208227 // Validating the model properties
209228 validate () {
210229 this .clearValidationErrors ();
211230
212- each ( this .$children , (child ) => {
231+ this .$children . forEach ( (child ) => {
213232 if (isFunction (child .validate ))
214233 {
215- let err = child .validate ();
216- each (err, (err ) => {
234+ let errors = child .validate (true );
235+ errors . forEach ( (err ) => {
217236 this .errors .push ({
218237 field: child .schema ,
219238 error: err
222241 }
223242 });
224243
225- return this .errors .length == 0 ;
244+ let isValid = this .errors .length == 0 ;
245+ this .$emit (" validated" , isValid, this .errors );
246+ return isValid;
226247 },
227248
228249 // Clear validation errors
242263 return field .buttons && field .buttons .length > 0 ;
243264 },
244265
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 );
247269 }
248270 }
249271 };
0 commit comments