diff --git a/src/angular/services/validation.service.ts b/src/angular/services/validation.service.ts index c5c3e293ae1..c5857221db2 100644 --- a/src/angular/services/validation.service.ts +++ b/src/angular/services/validation.service.ts @@ -3,6 +3,8 @@ import { Injectable } from '@angular/core'; import { I18nService } from '../../abstractions/i18n.service'; import { PlatformUtilsService } from '../../abstractions/platformUtils.service'; +import { ErrorResponse } from '../../models/response/errorResponse'; + @Injectable() export class ValidationService { constructor(private i18nService: I18nService, private platformUtilsService: PlatformUtilsService) { } @@ -15,23 +17,10 @@ export class ValidationService { errors.push(data); } else if (data == null || typeof data !== 'object') { errors.push(defaultErrorMessage); - } else if (data.validationErrors == null) { - errors.push(data.message ? data.message : defaultErrorMessage); + } else if (data.validationErrors != null) { + errors.concat((data as ErrorResponse).getAllMessages()); } else { - for (const key in data.validationErrors) { - if (!data.validationErrors.hasOwnProperty(key)) { - continue; - } - - data.validationErrors[key].forEach((item: string) => { - let prefix = ''; - if (key.indexOf('[') > -1 && key.indexOf(']') > -1) { - const lastSep = key.lastIndexOf('.'); - prefix = key.substr(0, lastSep > -1 ? lastSep : key.length) + ': '; - } - errors.push(prefix + item); - }); - } + errors.push(data.message ? data.message : defaultErrorMessage); } if (errors.length === 1) { diff --git a/src/models/response/errorResponse.ts b/src/models/response/errorResponse.ts index 74abda15f40..87afde4837b 100644 --- a/src/models/response/errorResponse.ts +++ b/src/models/response/errorResponse.ts @@ -23,16 +23,38 @@ export class ErrorResponse { } getSingleMessage(): string { - if (this.validationErrors) { - for (const key in this.validationErrors) { - if (!this.validationErrors.hasOwnProperty(key)) { - continue; - } - if (this.validationErrors[key].length) { - return this.validationErrors[key][0]; - } + if (this.validationErrors == null) { + return this.message; + } + for (const key in this.validationErrors) { + if (!this.validationErrors.hasOwnProperty(key)) { + continue; + } + if (this.validationErrors[key].length) { + return this.validationErrors[key][0]; } } return this.message; } + + getAllMessages(): string[] { + const messages: string[] = []; + if (this.validationErrors == null) { + return messages; + } + for (const key in this.validationErrors) { + if (!this.validationErrors.hasOwnProperty(key)) { + continue; + } + this.validationErrors[key].forEach((item: string) => { + let prefix = ''; + if (key.indexOf('[') > -1 && key.indexOf(']') > -1) { + const lastSep = key.lastIndexOf('.'); + prefix = key.substr(0, lastSep > -1 ? lastSep : key.length) + ': '; + } + messages.push(prefix + item); + }); + } + return messages; + } }