2015-07-17 13:21:37 -07:00
|
|
|
import {Injectable} from 'angular2/di';
|
2015-08-20 14:28:25 -07:00
|
|
|
import {StringMapWrapper, ListWrapper, List} from 'angular2/src/core/facade/collection';
|
|
|
|
import {isPresent, isArray} from 'angular2/src/core/facade/lang';
|
2015-03-09 17:41:49 +01:00
|
|
|
import * as modelModule from './model';
|
2015-03-10 18:12:50 -07:00
|
|
|
|
|
|
|
|
2015-03-31 22:47:11 +00:00
|
|
|
/**
|
2015-04-10 11:15:01 -07:00
|
|
|
* Creates a form object from a user-specified configuration.
|
|
|
|
*
|
|
|
|
* # Example
|
|
|
|
*
|
2015-04-29 10:33:20 -05:00
|
|
|
* ```
|
|
|
|
* import {Component, View, bootstrap} from 'angular2/angular2';
|
2015-08-10 21:42:47 -07:00
|
|
|
* import {FormBuilder, Validators, FORM_DIRECTIVES, ControlGroup} from 'angular2/forms';
|
2015-05-11 15:20:04 +02:00
|
|
|
*
|
2015-04-29 10:33:20 -05:00
|
|
|
* @Component({
|
|
|
|
* selector: 'login-comp',
|
2015-07-29 15:01:22 -07:00
|
|
|
* viewBindings: [
|
2015-04-29 10:33:20 -05:00
|
|
|
* FormBuilder
|
|
|
|
* ]
|
|
|
|
* })
|
|
|
|
* @View({
|
|
|
|
* template: `
|
|
|
|
* <form [control-group]="loginForm">
|
|
|
|
* Login <input control="login">
|
2015-05-11 15:20:04 +02:00
|
|
|
*
|
2015-04-29 10:33:20 -05:00
|
|
|
* <div control-group="passwordRetry">
|
|
|
|
* Password <input type="password" control="password">
|
|
|
|
* Confirm password <input type="password" control="passwordConfirmation">
|
|
|
|
* </div>
|
|
|
|
* </form>
|
|
|
|
* `,
|
|
|
|
* directives: [
|
2015-08-10 21:42:47 -07:00
|
|
|
* FORM_DIRECTIVES
|
2015-04-29 10:33:20 -05:00
|
|
|
* ]
|
|
|
|
* })
|
|
|
|
* class LoginComp {
|
|
|
|
* loginForm: ControlGroup;
|
2015-05-11 15:20:04 +02:00
|
|
|
*
|
2015-04-29 10:33:20 -05:00
|
|
|
* constructor(builder: FormBuilder) {
|
|
|
|
* this.loginForm = builder.group({
|
|
|
|
* login: ["", Validators.required],
|
2015-05-11 15:20:04 +02:00
|
|
|
*
|
2015-04-29 10:33:20 -05:00
|
|
|
* passwordRetry: builder.group({
|
|
|
|
* password: ["", Validators.required],
|
|
|
|
* passwordConfirmation: ["", Validators.required]
|
|
|
|
* })
|
|
|
|
* });
|
|
|
|
* }
|
|
|
|
* }
|
2015-05-11 15:20:04 +02:00
|
|
|
*
|
2015-04-29 10:33:20 -05:00
|
|
|
* bootstrap(LoginComp)
|
|
|
|
* ```
|
2015-05-11 15:20:04 +02:00
|
|
|
*
|
2015-05-20 18:10:30 -07:00
|
|
|
* This example creates a {@link ControlGroup} that consists of a `login` {@link Control}, and a
|
|
|
|
* nested
|
2015-04-29 10:33:20 -05:00
|
|
|
* {@link ControlGroup} that defines a `password` and a `passwordConfirmation` {@link Control}:
|
2015-04-10 11:15:01 -07:00
|
|
|
*
|
|
|
|
* ```
|
|
|
|
* var loginForm = builder.group({
|
|
|
|
* login: ["", Validators.required],
|
|
|
|
*
|
|
|
|
* passwordRetry: builder.group({
|
|
|
|
* password: ["", Validators.required],
|
|
|
|
* passwordConfirmation: ["", Validators.required]
|
|
|
|
* })
|
|
|
|
* });
|
|
|
|
*
|
|
|
|
* ```
|
2015-03-31 22:47:11 +00:00
|
|
|
*/
|
2015-07-17 13:21:37 -07:00
|
|
|
@Injectable()
|
2015-03-10 18:12:50 -07:00
|
|
|
export class FormBuilder {
|
2015-05-20 18:10:30 -07:00
|
|
|
group(controlsConfig: StringMap<string, any>,
|
|
|
|
extra: StringMap<string, any> = null): modelModule.ControlGroup {
|
2015-03-10 18:12:50 -07:00
|
|
|
var controls = this._reduceControls(controlsConfig);
|
|
|
|
var optionals = isPresent(extra) ? StringMapWrapper.get(extra, "optionals") : null;
|
|
|
|
var validator = isPresent(extra) ? StringMapWrapper.get(extra, "validator") : null;
|
|
|
|
|
|
|
|
if (isPresent(validator)) {
|
2015-03-09 17:41:49 +01:00
|
|
|
return new modelModule.ControlGroup(controls, optionals, validator);
|
2015-03-10 18:12:50 -07:00
|
|
|
} else {
|
2015-03-09 17:41:49 +01:00
|
|
|
return new modelModule.ControlGroup(controls, optionals);
|
2015-03-10 18:12:50 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-20 18:10:30 -07:00
|
|
|
control(value: Object, validator: Function = null): modelModule.Control {
|
2015-03-10 18:12:50 -07:00
|
|
|
if (isPresent(validator)) {
|
2015-03-09 17:41:49 +01:00
|
|
|
return new modelModule.Control(value, validator);
|
2015-03-10 18:12:50 -07:00
|
|
|
} else {
|
2015-03-09 17:41:49 +01:00
|
|
|
return new modelModule.Control(value);
|
2015-03-10 18:12:50 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-20 18:10:30 -07:00
|
|
|
array(controlsConfig: List<any>, validator: Function = null): modelModule.ControlArray {
|
2015-03-25 10:51:05 -07:00
|
|
|
var controls = ListWrapper.map(controlsConfig, (c) => this._createControl(c));
|
|
|
|
if (isPresent(validator)) {
|
|
|
|
return new modelModule.ControlArray(controls, validator);
|
|
|
|
} else {
|
|
|
|
return new modelModule.ControlArray(controls);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-20 18:10:30 -07:00
|
|
|
_reduceControls(controlsConfig: any): StringMap<string, modelModule.AbstractControl> {
|
2015-03-10 18:12:50 -07:00
|
|
|
var controls = {};
|
|
|
|
StringMapWrapper.forEach(controlsConfig, (controlConfig, controlName) => {
|
|
|
|
controls[controlName] = this._createControl(controlConfig);
|
|
|
|
});
|
|
|
|
return controls;
|
|
|
|
}
|
|
|
|
|
2015-05-20 18:10:30 -07:00
|
|
|
_createControl(controlConfig: any): modelModule.AbstractControl {
|
2015-07-10 11:29:41 +02:00
|
|
|
if (controlConfig instanceof modelModule.Control ||
|
|
|
|
controlConfig instanceof modelModule.ControlGroup ||
|
|
|
|
controlConfig instanceof modelModule.ControlArray) {
|
2015-03-10 18:12:50 -07:00
|
|
|
return controlConfig;
|
|
|
|
|
2015-06-11 19:32:55 +02:00
|
|
|
} else if (isArray(controlConfig)) {
|
2015-03-10 18:12:50 -07:00
|
|
|
var value = ListWrapper.get(controlConfig, 0);
|
|
|
|
var validator = controlConfig.length > 1 ? controlConfig[1] : null;
|
|
|
|
return this.control(value, validator);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
return this.control(controlConfig);
|
|
|
|
}
|
|
|
|
}
|
2015-04-10 11:15:01 -07:00
|
|
|
}
|