Skip to content

Commit 2261c99

Browse files
Preparing to move the ASP.NET MVC validation result client-side code into a separate NPM module
1 parent 906a17e commit 2261c99

2 files changed

Lines changed: 42 additions & 19 deletions

File tree

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { ControlGroup } from 'angular2/angular2';
2+
import { Response } from 'angular2/http';
3+
4+
// TODO: Factor this out into a separate NPM module
5+
export class Validation {
6+
7+
public static showValidationErrors(response: ValidationErrorResult | Response, controlGroup: ControlGroup): void {
8+
if (response instanceof Response) {
9+
var httpResponse = <Response>response;
10+
response = <ValidationErrorResult>(httpResponse.json());
11+
}
12+
13+
// It's not yet clear whether this is a legitimate and supported use of the ng.ControlGroup API.
14+
// Need feedback from the Angular 2 team on whether there's a better way.
15+
var errors = <ValidationErrorResult>response;
16+
Object.keys(errors || {}).forEach(key => {
17+
errors[key].forEach(errorMessage => {
18+
// This in particular is rough
19+
if (!controlGroup.controls[key].errors) {
20+
(<any>controlGroup.controls[key])._errors = {};
21+
}
22+
23+
controlGroup.controls[key].errors[errorMessage] = true;
24+
});
25+
});
26+
}
27+
28+
}
29+
30+
export interface ValidationErrorResult {
31+
[propertyName: string]: string[];
32+
}

samples/angular/MusicStore/wwwroot/ng-app/components/admin/album-edit/album-edit.ts

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import * as models from '../../../models/models';
44
import { Http, HTTP_BINDINGS, Headers, Response } from 'angular2/http';
55
import { AlbumDeletePrompt } from '../album-delete-prompt/album-delete-prompt';
66
import { FormField } from '../form-field/form-field';
7+
import * as AspNet from './AspNetUtil';
78

89
@ng.Component({
910
selector: 'album-edit'
@@ -67,21 +68,11 @@ export class AlbumEdit {
6768
var controls = this.form.controls;
6869
var albumId = this.originalAlbum.AlbumId;
6970

70-
this._putJson(`/api/albums/${ albumId }/update`, this.form.value).then(response => {
71+
this._putJson(`/api/albums/${ albumId }/update`, this.form.value).subscribe(response => {
7172
if (response.status === 200) {
7273
this.changesSaved = true;
7374
} else {
74-
var errors = <ValidationResponse>(response.json());
75-
Object.keys(errors).forEach(key => {
76-
errors[key].forEach(errorMessage => {
77-
// TODO: There has to be a better API for this
78-
if (!this.form.controls[key].errors) {
79-
(<any>this.form.controls[key])._errors = {};
80-
}
81-
82-
this.form.controls[key].errors[errorMessage] = true;
83-
});
84-
});
75+
AspNet.Validation.showValidationErrors(response, this.form);
8576
}
8677
});
8778
}
@@ -91,15 +82,15 @@ export class AlbumEdit {
9182
return /^\d+\.\d+$/.test(control.value) ? null : { Price: true };
9283
}
9384

94-
private _putJson(url: string, body: any): Promise<Response> {
95-
return new Promise((resolve, reject) => {
96-
this._http.put(url, JSON.stringify(body), {
97-
headers: new Headers({ 'Content-Type': 'application/json' })
98-
}).subscribe(resolve);
85+
// Need feedback on whether this really is the easiest way to PUT some JSON
86+
private _putJson(url: string, body: any): Subscribable<Response> {
87+
return this._http.put(url, JSON.stringify(body), {
88+
headers: new Headers({ 'Content-Type': 'application/json' })
9989
});
10090
}
10191
}
10292

103-
interface ValidationResponse {
104-
[propertyName: string]: string[];
93+
// TODO: Figure out what type declaration is provided by Angular/RxJs and use that instead
94+
interface Subscribable<T> {
95+
subscribe(callback: (response: Response) => void): void;
10596
}

0 commit comments

Comments
 (0)