Beginning angular2-aspnet NPM package

This commit is contained in:
SteveSandersonMS
2015-12-10 14:10:21 +00:00
parent 2261c9964e
commit 30281636d6
7 changed files with 107 additions and 0 deletions

View File

@@ -0,0 +1 @@
export * from './Validation';

View File

@@ -0,0 +1,31 @@
import { ControlGroup } from 'angular2/angular2';
import { Response } from 'angular2/http';
export class Validation {
public static showValidationErrors(response: ValidationErrorResult | Response, controlGroup: ControlGroup): void {
if (response instanceof Response) {
var httpResponse = <Response>response;
response = <ValidationErrorResult>(httpResponse.json());
}
// It's not yet clear whether this is a legitimate and supported use of the ng.ControlGroup API.
// Need feedback from the Angular 2 team on whether there's a better way.
var errors = <ValidationErrorResult>response;
Object.keys(errors || {}).forEach(key => {
errors[key].forEach(errorMessage => {
// This in particular is rough
if (!controlGroup.controls[key].errors) {
(<any>controlGroup.controls[key])._errors = {};
}
controlGroup.controls[key].errors[errorMessage] = true;
});
});
}
}
export interface ValidationErrorResult {
[propertyName: string]: string[];
}