2016-05-19 14:31:21 -07:00
|
|
|
import {bootstrap} from '@angular/platform-browser';
|
2016-04-12 09:40:37 -07:00
|
|
|
import {
|
|
|
|
FORM_DIRECTIVES,
|
|
|
|
ControlGroup,
|
|
|
|
Validators,
|
|
|
|
NgFormModel,
|
|
|
|
FormBuilder,
|
|
|
|
NgIf,
|
|
|
|
NgFor
|
2016-04-28 17:50:03 -07:00
|
|
|
} from '@angular/common';
|
|
|
|
import {Component, Directive, Host} from '@angular/core';
|
2015-06-05 15:27:46 -07:00
|
|
|
|
2016-05-01 22:54:19 -07:00
|
|
|
import {RegExpWrapper, print, isPresent} from '@angular/core/src/facade/lang';
|
2016-04-28 17:50:03 -07:00
|
|
|
import {AbstractControl} from '@angular/common';
|
2015-06-05 15:27:46 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Custom validator.
|
|
|
|
*/
|
2016-02-19 11:49:31 -08:00
|
|
|
function creditCardValidator(c: AbstractControl): {[key: string]: boolean} {
|
2015-09-01 20:59:43 -07:00
|
|
|
if (isPresent(c.value) && RegExpWrapper.test(/^\d{16}$/g, c.value)) {
|
2015-06-05 15:27:46 -07:00
|
|
|
return null;
|
|
|
|
} else {
|
2016-04-12 09:40:37 -07:00
|
|
|
return {"invalidCreditCard": true};
|
2015-06-05 15:27:46 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This is a component that displays an error message.
|
|
|
|
*
|
|
|
|
* For instance,
|
|
|
|
*
|
|
|
|
* <show-error control="creditCard" [errors]="['required', 'invalidCreditCard']"></show-error>
|
|
|
|
*
|
|
|
|
* Will display the "is required" error if the control is empty, and "invalid credit card" if the
|
|
|
|
* control is not empty
|
|
|
|
* but not valid.
|
|
|
|
*
|
|
|
|
* In a real application, this component would receive a service that would map an error code to an
|
|
|
|
* actual error message.
|
|
|
|
* To make it simple, we are using a simple map here.
|
|
|
|
*/
|
2016-03-08 13:36:48 -08:00
|
|
|
@Component({
|
|
|
|
selector: 'show-error',
|
|
|
|
inputs: ['controlPath: control', 'errorTypes: errors'],
|
2015-06-05 15:27:46 -07:00
|
|
|
template: `
|
2015-11-23 16:02:19 -08:00
|
|
|
<span *ngIf="errorMessage !== null">{{errorMessage}}</span>
|
2015-06-05 15:27:46 -07:00
|
|
|
`,
|
|
|
|
directives: [NgIf]
|
|
|
|
})
|
|
|
|
class ShowError {
|
|
|
|
formDir;
|
|
|
|
controlPath: string;
|
2015-08-28 11:29:19 -07:00
|
|
|
errorTypes: string[];
|
2015-06-05 15:27:46 -07:00
|
|
|
|
2015-07-29 11:26:09 -07:00
|
|
|
constructor(@Host() formDir: NgFormModel) { this.formDir = formDir; }
|
2015-06-05 15:27:46 -07:00
|
|
|
|
2015-09-01 20:59:43 -07:00
|
|
|
get errorMessage(): string {
|
2015-10-09 09:07:58 -07:00
|
|
|
var form: ControlGroup = this.formDir.form;
|
|
|
|
var control = form.find(this.controlPath);
|
2015-09-01 20:59:43 -07:00
|
|
|
if (isPresent(control) && control.touched) {
|
|
|
|
for (var i = 0; i < this.errorTypes.length; ++i) {
|
|
|
|
if (control.hasError(this.errorTypes[i])) {
|
|
|
|
return this._errorMessage(this.errorTypes[i]);
|
|
|
|
}
|
2015-06-05 15:27:46 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2015-09-01 20:59:43 -07:00
|
|
|
_errorMessage(code: string): string {
|
2015-06-05 15:27:46 -07:00
|
|
|
var config = {'required': 'is required', 'invalidCreditCard': 'is invalid credit card number'};
|
|
|
|
return config[code];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-03-08 13:36:48 -08:00
|
|
|
@Component({
|
|
|
|
selector: 'model-driven-forms',
|
|
|
|
viewProviders: [FormBuilder],
|
2015-06-05 15:27:46 -07:00
|
|
|
template: `
|
|
|
|
<h1>Checkout Form (Model Driven)</h1>
|
|
|
|
|
2015-11-23 16:02:19 -08:00
|
|
|
<form (ngSubmit)="onSubmit()" [ngFormModel]="form" #f="ngForm">
|
2015-06-05 15:27:46 -07:00
|
|
|
<p>
|
|
|
|
<label for="firstName">First Name</label>
|
2015-11-23 16:02:19 -08:00
|
|
|
<input type="text" id="firstName" ngControl="firstName">
|
2015-06-05 15:27:46 -07:00
|
|
|
<show-error control="firstName" [errors]="['required']"></show-error>
|
|
|
|
</p>
|
|
|
|
|
|
|
|
<p>
|
|
|
|
<label for="middleName">Middle Name</label>
|
2015-11-23 16:02:19 -08:00
|
|
|
<input type="text" id="middleName" ngControl="middleName">
|
2015-06-05 15:27:46 -07:00
|
|
|
</p>
|
|
|
|
|
|
|
|
<p>
|
|
|
|
<label for="lastName">Last Name</label>
|
2015-11-23 16:02:19 -08:00
|
|
|
<input type="text" id="lastName" ngControl="lastName">
|
2015-06-05 15:27:46 -07:00
|
|
|
<show-error control="lastName" [errors]="['required']"></show-error>
|
|
|
|
</p>
|
|
|
|
|
2015-06-13 12:15:54 -07:00
|
|
|
<p>
|
|
|
|
<label for="country">Country</label>
|
2015-11-23 16:02:19 -08:00
|
|
|
<select id="country" ngControl="country">
|
2016-04-25 19:52:24 -07:00
|
|
|
<option *ngFor="let c of countries" [value]="c">{{c}}</option>
|
2015-06-13 12:15:54 -07:00
|
|
|
</select>
|
|
|
|
</p>
|
|
|
|
|
2015-06-05 15:27:46 -07:00
|
|
|
<p>
|
|
|
|
<label for="creditCard">Credit Card</label>
|
2015-11-23 16:02:19 -08:00
|
|
|
<input type="text" id="creditCard" ngControl="creditCard">
|
2015-06-05 15:27:46 -07:00
|
|
|
<show-error control="creditCard" [errors]="['required', 'invalidCreditCard']"></show-error>
|
|
|
|
</p>
|
|
|
|
|
|
|
|
<p>
|
|
|
|
<label for="amount">Amount</label>
|
2015-11-23 16:02:19 -08:00
|
|
|
<input type="number" id="amount" ngControl="amount">
|
2015-06-05 15:27:46 -07:00
|
|
|
<show-error control="amount" [errors]="['required']"></show-error>
|
|
|
|
</p>
|
|
|
|
|
|
|
|
<p>
|
|
|
|
<label for="email">Email</label>
|
2015-11-23 16:02:19 -08:00
|
|
|
<input type="email" id="email" ngControl="email">
|
2015-06-05 15:27:46 -07:00
|
|
|
<show-error control="email" [errors]="['required']"></show-error>
|
|
|
|
</p>
|
|
|
|
|
|
|
|
<p>
|
|
|
|
<label for="comments">Comments</label>
|
2015-11-23 16:02:19 -08:00
|
|
|
<textarea id="comments" ngControl="comments">
|
2015-06-05 15:27:46 -07:00
|
|
|
</textarea>
|
|
|
|
</p>
|
|
|
|
|
|
|
|
<button type="submit" [disabled]="!f.form.valid">Submit</button>
|
|
|
|
</form>
|
|
|
|
`,
|
2015-08-10 21:42:47 -07:00
|
|
|
directives: [FORM_DIRECTIVES, NgFor, ShowError]
|
2015-06-05 15:27:46 -07:00
|
|
|
})
|
|
|
|
class ModelDrivenForms {
|
|
|
|
form;
|
2015-06-13 12:15:54 -07:00
|
|
|
countries = ['US', 'Canada'];
|
2015-06-05 15:27:46 -07:00
|
|
|
|
|
|
|
constructor(fb: FormBuilder) {
|
|
|
|
this.form = fb.group({
|
2016-04-12 09:40:37 -07:00
|
|
|
"firstName": ["", Validators.required],
|
|
|
|
"middleName": [""],
|
|
|
|
"lastName": ["", Validators.required],
|
|
|
|
"country": ["Canada", Validators.required],
|
|
|
|
"creditCard": ["", Validators.compose([Validators.required, creditCardValidator])],
|
|
|
|
"amount": [0, Validators.required],
|
|
|
|
"email": ["", Validators.required],
|
|
|
|
"comments": [""]
|
2015-06-05 15:27:46 -07:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2015-09-01 20:59:43 -07:00
|
|
|
onSubmit(): void {
|
2016-04-12 09:40:37 -07:00
|
|
|
print("Submitting:");
|
2015-06-05 15:27:46 -07:00
|
|
|
print(this.form.value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function main() {
|
|
|
|
bootstrap(ModelDrivenForms);
|
|
|
|
}
|