mirror of
https://github.com/aspnet/JavaScriptServices.git
synced 2025-12-23 01:58:29 +00:00
Preparing to move the ASP.NET MVC validation result client-side code into a separate NPM module
This commit is contained in:
@@ -0,0 +1,32 @@
|
|||||||
|
import { ControlGroup } from 'angular2/angular2';
|
||||||
|
import { Response } from 'angular2/http';
|
||||||
|
|
||||||
|
// TODO: Factor this out into a separate NPM module
|
||||||
|
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[];
|
||||||
|
}
|
||||||
@@ -4,6 +4,7 @@ import * as models from '../../../models/models';
|
|||||||
import { Http, HTTP_BINDINGS, Headers, Response } from 'angular2/http';
|
import { Http, HTTP_BINDINGS, Headers, Response } from 'angular2/http';
|
||||||
import { AlbumDeletePrompt } from '../album-delete-prompt/album-delete-prompt';
|
import { AlbumDeletePrompt } from '../album-delete-prompt/album-delete-prompt';
|
||||||
import { FormField } from '../form-field/form-field';
|
import { FormField } from '../form-field/form-field';
|
||||||
|
import * as AspNet from './AspNetUtil';
|
||||||
|
|
||||||
@ng.Component({
|
@ng.Component({
|
||||||
selector: 'album-edit'
|
selector: 'album-edit'
|
||||||
@@ -67,21 +68,11 @@ export class AlbumEdit {
|
|||||||
var controls = this.form.controls;
|
var controls = this.form.controls;
|
||||||
var albumId = this.originalAlbum.AlbumId;
|
var albumId = this.originalAlbum.AlbumId;
|
||||||
|
|
||||||
this._putJson(`/api/albums/${ albumId }/update`, this.form.value).then(response => {
|
this._putJson(`/api/albums/${ albumId }/update`, this.form.value).subscribe(response => {
|
||||||
if (response.status === 200) {
|
if (response.status === 200) {
|
||||||
this.changesSaved = true;
|
this.changesSaved = true;
|
||||||
} else {
|
} else {
|
||||||
var errors = <ValidationResponse>(response.json());
|
AspNet.Validation.showValidationErrors(response, this.form);
|
||||||
Object.keys(errors).forEach(key => {
|
|
||||||
errors[key].forEach(errorMessage => {
|
|
||||||
// TODO: There has to be a better API for this
|
|
||||||
if (!this.form.controls[key].errors) {
|
|
||||||
(<any>this.form.controls[key])._errors = {};
|
|
||||||
}
|
|
||||||
|
|
||||||
this.form.controls[key].errors[errorMessage] = true;
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -91,15 +82,15 @@ export class AlbumEdit {
|
|||||||
return /^\d+\.\d+$/.test(control.value) ? null : { Price: true };
|
return /^\d+\.\d+$/.test(control.value) ? null : { Price: true };
|
||||||
}
|
}
|
||||||
|
|
||||||
private _putJson(url: string, body: any): Promise<Response> {
|
// Need feedback on whether this really is the easiest way to PUT some JSON
|
||||||
return new Promise((resolve, reject) => {
|
private _putJson(url: string, body: any): Subscribable<Response> {
|
||||||
this._http.put(url, JSON.stringify(body), {
|
return this._http.put(url, JSON.stringify(body), {
|
||||||
headers: new Headers({ 'Content-Type': 'application/json' })
|
headers: new Headers({ 'Content-Type': 'application/json' })
|
||||||
}).subscribe(resolve);
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
interface ValidationResponse {
|
// TODO: Figure out what type declaration is provided by Angular/RxJs and use that instead
|
||||||
[propertyName: string]: string[];
|
interface Subscribable<T> {
|
||||||
|
subscribe(callback: (response: Response) => void): void;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user