Files
Angular-Cn/modules/@angular/common/src/forms-deprecated/form_builder.ts
T

124 lines
4.2 KiB
TypeScript
Raw Normal View History

/**
* @license
* Copyright Google Inc. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import {Injectable} from '@angular/core';
2016-06-08 16:38:52 -07:00
2016-05-31 15:22:59 -07:00
import {StringMapWrapper} from '../facade/collection';
2016-06-08 16:38:52 -07:00
import {isArray, isPresent} from '../facade/lang';
import {AsyncValidatorFn, ValidatorFn} from './directives/validators';
2016-06-22 14:56:10 -07:00
import {AbstractControl, Control, ControlArray, ControlGroup} from './model';
2016-06-08 16:38:52 -07:00
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 ([live demo](http://plnkr.co/edit/ENgZo8EuIECZNensZCVr?p=preview))
*
* ```typescript
2015-04-29 10:33:20 -05:00
* @Component({
* selector: 'my-app',
* viewProviders: [FORM_BINDINGS]
2015-04-29 10:33:20 -05:00
* template: `
* <form [ngFormModel]="loginForm">
* <p>Login <input ngControl="login"></p>
* <div ngControlGroup="passwordRetry">
* <p>Password <input type="password" ngControl="password"></p>
* <p>Confirm password <input type="password" ngControl="passwordConfirmation"></p>
2015-04-29 10:33:20 -05:00
* </div>
* </form>
* <h3>Form value:</h3>
* <pre>{{value}}</pre>
2015-04-29 10:33:20 -05:00
* `,
2015-09-01 20:59:43 -07:00
* directives: [FORM_DIRECTIVES]
2015-04-29 10:33:20 -05:00
* })
* export class App {
2015-04-29 10:33:20 -05:00
* loginForm: ControlGroup;
*
2015-04-29 10:33:20 -05:00
* constructor(builder: FormBuilder) {
* this.loginForm = builder.group({
* login: ["", Validators.required],
* passwordRetry: builder.group({
* password: ["", Validators.required],
* passwordConfirmation: ["", Validators.required, asyncValidator]
2015-04-29 10:33:20 -05:00
* })
* });
* }
*
* get value(): string {
* return JSON.stringify(this.loginForm.value, null, 2);
* }
2015-04-29 10:33:20 -05:00
* }
* ```
*
* @experimental
2015-03-31 22:47:11 +00:00
*/
@Injectable()
2015-03-10 18:12:50 -07:00
export class FormBuilder {
/**
* Construct a new {@link ControlGroup} with the given map of configuration.
* Valid keys for the `extra` parameter map are `optionals` and `validator`.
*
* See the {@link ControlGroup} constructor for more details.
*/
2016-06-22 14:56:10 -07:00
group(controlsConfig: {[key: string]: any}, extra: {[key: string]: any} = null): ControlGroup {
2015-03-10 18:12:50 -07:00
var controls = this._reduceControls(controlsConfig);
2016-02-19 11:49:31 -08:00
var optionals = <{[key: string]: boolean}>(
2016-06-08 16:38:52 -07:00
isPresent(extra) ? StringMapWrapper.get(extra, 'optionals') : null);
var validator: ValidatorFn = isPresent(extra) ? StringMapWrapper.get(extra, 'validator') : null;
2016-02-19 11:49:31 -08:00
var asyncValidator: AsyncValidatorFn =
2016-06-08 16:38:52 -07:00
isPresent(extra) ? StringMapWrapper.get(extra, 'asyncValidator') : null;
2016-06-22 14:56:10 -07:00
return new ControlGroup(controls, optionals, validator, asyncValidator);
2015-03-10 18:12:50 -07:00
}
/**
* Construct a new {@link Control} with the given `value`,`validator`, and `asyncValidator`.
*/
2016-06-08 16:38:52 -07:00
control(value: Object, validator: ValidatorFn = null, asyncValidator: AsyncValidatorFn = null):
2016-06-22 14:56:10 -07:00
Control {
return new Control(value, validator, asyncValidator);
2015-03-10 18:12:50 -07:00
}
/**
* Construct an array of {@link Control}s from the given `controlsConfig` array of
* configuration, with the given optional `validator` and `asyncValidator`.
*/
2016-06-08 16:38:52 -07:00
array(
controlsConfig: any[], validator: ValidatorFn = null,
2016-06-22 14:56:10 -07:00
asyncValidator: AsyncValidatorFn = null): ControlArray {
var controls = controlsConfig.map(c => this._createControl(c));
2016-06-22 14:56:10 -07:00
return new ControlArray(controls, validator, asyncValidator);
}
/** @internal */
2016-06-22 14:56:10 -07:00
_reduceControls(controlsConfig: {[k: string]: any}): {[key: string]: AbstractControl} {
var controls: {[key: string]: AbstractControl} = {};
2016-02-11 17:01:17 -08:00
StringMapWrapper.forEach(controlsConfig, (controlConfig: any, controlName: string) => {
2015-03-10 18:12:50 -07:00
controls[controlName] = this._createControl(controlConfig);
});
return controls;
}
/** @internal */
2016-06-22 14:56:10 -07:00
_createControl(controlConfig: any): AbstractControl {
if (controlConfig instanceof Control || controlConfig instanceof ControlGroup ||
controlConfig instanceof ControlArray) {
2015-03-10 18:12:50 -07:00
return controlConfig;
} else if (isArray(controlConfig)) {
2015-08-28 11:29:19 -07:00
var value = controlConfig[0];
2016-02-19 11:49:31 -08:00
var validator: ValidatorFn = controlConfig.length > 1 ? controlConfig[1] : null;
var asyncValidator: AsyncValidatorFn = controlConfig.length > 2 ? controlConfig[2] : null;
return this.control(value, validator, asyncValidator);
2015-03-10 18:12:50 -07:00
} else {
return this.control(controlConfig);
}
}
2016-02-19 11:49:31 -08:00
}