mirror of
https://github.com/aspnet/JavaScriptServices.git
synced 2025-12-24 18:47:30 +00:00
Beginning angular2-aspnet NPM package
This commit is contained in:
1
Microsoft.AspNet.AngularServices/npm/src/Exports.ts
Normal file
1
Microsoft.AspNet.AngularServices/npm/src/Exports.ts
Normal file
@@ -0,0 +1 @@
|
||||
export * from './Validation';
|
||||
31
Microsoft.AspNet.AngularServices/npm/src/Validation.ts
Normal file
31
Microsoft.AspNet.AngularServices/npm/src/Validation.ts
Normal 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[];
|
||||
}
|
||||
Reference in New Issue
Block a user