-
Notifications
You must be signed in to change notification settings - Fork 27.2k
feat(forms): implement setErrors #4917
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -46,22 +46,20 @@ function _find(control: AbstractControl, path: Array<string | number>| string) { | |
| /** | ||
| * | ||
| */ | ||
| export class AbstractControl { | ||
| export abstract class AbstractControl { | ||
| /** @internal */ | ||
| _value: any; | ||
| /** @internal */ | ||
| _status: string; | ||
| /** @internal */ | ||
| _errors: {[key: string]: any}; | ||
| /** @internal */ | ||
| _pristine: boolean = true; | ||
| /** @internal */ | ||
| _touched: boolean = false; | ||
| /** @internal */ | ||
| _parent: ControlGroup | ControlArray; | ||
|
|
||
| /** @internal */ | ||
| _valueChanges: EventEmitter; | ||
|
|
||
| private _status: string; | ||
| private _errors: {[key: string]: any}; | ||
| private _controlsErrors: any; | ||
| private _pristine: boolean = true; | ||
| private _touched: boolean = false; | ||
| private _parent: ControlGroup | ControlArray; | ||
|
|
||
| constructor(public validator: Function) {} | ||
|
|
||
| get value(): any { return this._value; } | ||
|
|
@@ -70,8 +68,16 @@ export class AbstractControl { | |
|
|
||
| get valid(): boolean { return this._status === VALID; } | ||
|
|
||
| /** | ||
| * Returns the errors of this control. | ||
| */ | ||
| get errors(): {[key: string]: any} { return this._errors; } | ||
|
|
||
| /** | ||
| * Returns the errors of the child controls. | ||
| */ | ||
| get controlsErrors(): any { return this._controlsErrors; } | ||
|
|
||
| get pristine(): boolean { return this._pristine; } | ||
|
|
||
| get dirty(): boolean { return !this.pristine; } | ||
|
|
@@ -105,17 +111,6 @@ export class AbstractControl { | |
|
|
||
| setParent(parent: ControlGroup | ControlArray): void { this._parent = parent; } | ||
|
|
||
| updateValidity({onlySelf}: {onlySelf?: boolean} = {}): void { | ||
| onlySelf = normalizeBool(onlySelf); | ||
|
|
||
| this._errors = this.validator(this); | ||
| this._status = isPresent(this._errors) ? INVALID : VALID; | ||
|
|
||
| if (isPresent(this._parent) && !onlySelf) { | ||
| this._parent.updateValidity({onlySelf: onlySelf}); | ||
| } | ||
| } | ||
|
|
||
| updateValueAndValidity({onlySelf, emitEvent}: {onlySelf?: boolean, emitEvent?: boolean} = {}): | ||
| void { | ||
| onlySelf = normalizeBool(onlySelf); | ||
|
|
@@ -124,7 +119,8 @@ export class AbstractControl { | |
| this._updateValue(); | ||
|
|
||
| this._errors = this.validator(this); | ||
| this._status = isPresent(this._errors) ? INVALID : VALID; | ||
| this._controlsErrors = this._calculateControlsErrors(); | ||
| this._status = this._calculateStatus(); | ||
|
|
||
| if (emitEvent) { | ||
| ObservableWrapper.callNext(this._valueChanges, this._value); | ||
|
|
@@ -135,6 +131,38 @@ export class AbstractControl { | |
| } | ||
| } | ||
|
|
||
| /** | ||
| * Sets errors on a control. | ||
| * | ||
| * This is used when validations are run not automatically, but manually by the user. | ||
| * | ||
| * Calling `setErrors` will also update the validity of the parent control. | ||
| * | ||
| * ## Usage | ||
| * | ||
| * ``` | ||
| * var login = new Control("someLogin"); | ||
| * login.setErrors({ | ||
| * "notUnique": true | ||
| * }); | ||
| * | ||
| * expect(login.valid).toEqual(false); | ||
| * expect(login.errors).toEqual({"notUnique": true}); | ||
| * | ||
| * login.updateValue("someOtherLogin"); | ||
| * | ||
| * expect(login.valid).toEqual(true); | ||
| * ``` | ||
| */ | ||
| setErrors(errors: {[key: string]: any}): void { | ||
| this._errors = errors; | ||
| this._status = this._calculateStatus(); | ||
|
|
||
| if (isPresent(this._parent)) { | ||
| this._parent._updateControlsErrors(); | ||
| } | ||
| } | ||
|
|
||
| find(path: Array<string | number>| string): AbstractControl { return _find(this, path); } | ||
|
|
||
| getError(errorCode: string, path: string[] = null): any { | ||
|
|
@@ -151,7 +179,23 @@ export class AbstractControl { | |
| } | ||
|
|
||
| /** @internal */ | ||
| _updateValue(): void {} | ||
| _updateControlsErrors(): void { | ||
| this._controlsErrors = this._calculateControlsErrors(); | ||
| this._status = this._calculateStatus(); | ||
|
|
||
| if (isPresent(this._parent)) { | ||
| this._parent._updateControlsErrors(); | ||
| } | ||
| } | ||
|
|
||
| private _calculateStatus(): string { | ||
| return isPresent(this._errors) || isPresent(this._controlsErrors) ? INVALID : VALID; | ||
| } | ||
|
|
||
| /** @internal */ | ||
| abstract _updateValue(): void; | ||
| /** @internal */ | ||
| abstract _calculateControlsErrors(): any; | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -177,7 +221,7 @@ export class Control extends AbstractControl { | |
| constructor(value: any = null, validator: Function = Validators.nullValidator) { | ||
| super(validator); | ||
| this._value = value; | ||
| this.updateValidity({onlySelf: true}); | ||
| this.updateValueAndValidity({onlySelf: true, emitEvent: false}); | ||
| this._valueChanges = new EventEmitter(); | ||
| } | ||
|
|
||
|
|
@@ -203,6 +247,16 @@ export class Control extends AbstractControl { | |
| this.updateValueAndValidity({onlySelf: onlySelf, emitEvent: emitEvent}); | ||
| } | ||
|
|
||
| /** | ||
| * @internal | ||
| */ | ||
| _updateValue() {} | ||
|
|
||
| /** | ||
| * @internal | ||
| */ | ||
| _calculateControlsErrors() { return null; } | ||
|
|
||
| /** | ||
| * Register a listener for change events. | ||
| */ | ||
|
|
@@ -226,14 +280,14 @@ export class ControlGroup extends AbstractControl { | |
| private _optionals: {[key: string]: boolean}; | ||
|
|
||
| constructor(public controls: {[key: string]: AbstractControl}, | ||
| optionals: {[key: string]: boolean} = null, validator: Function = Validators.group) { | ||
| optionals: {[key: string]: boolean} = null, | ||
| validator: Function = Validators.nullValidator) { | ||
| super(validator); | ||
| this._optionals = isPresent(optionals) ? optionals : {}; | ||
| this._valueChanges = new EventEmitter(); | ||
|
|
||
| this._setParentForControls(); | ||
| this._value = this._reduceValue(); | ||
| this.updateValidity({onlySelf: true}); | ||
| this.updateValueAndValidity({onlySelf: true, emitEvent: false}); | ||
| } | ||
|
|
||
| addControl(name: string, control: AbstractControl): void { | ||
|
|
@@ -266,6 +320,9 @@ export class ControlGroup extends AbstractControl { | |
| /** @internal */ | ||
| _updateValue() { this._value = this._reduceValue(); } | ||
|
|
||
| /** @internal */ | ||
| _calculateControlsErrors() { return Validators.group(this); } | ||
|
|
||
| /** @internal */ | ||
| _reduceValue() { | ||
| return this._reduceChildren({}, (acc, control, name) => { | ||
|
|
@@ -314,14 +371,13 @@ export class ControlGroup extends AbstractControl { | |
| * ### Example ([live demo](http://plnkr.co/edit/23DESOpbNnBpBHZt1BR4?p=preview)) | ||
| */ | ||
| export class ControlArray extends AbstractControl { | ||
| constructor(public controls: AbstractControl[], validator: Function = Validators.array) { | ||
| constructor(public controls: AbstractControl[], validator: Function = Validators.nullValidator) { | ||
| super(validator); | ||
|
|
||
| this._valueChanges = new EventEmitter(); | ||
|
|
||
| this._setParentForControls(); | ||
| this._updateValue(); | ||
| this.updateValidity({onlySelf: true}); | ||
| this.updateValueAndValidity({onlySelf: true, emitEvent: false}); | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -363,6 +419,9 @@ export class ControlArray extends AbstractControl { | |
| /** @internal */ | ||
| _updateValue(): void { this._value = this.controls.map((control) => control.value); } | ||
|
|
||
| /** @internal */ | ||
| _calculateControlsErrors() { return Validators.array(this); } | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wonder if it makes sense to move group, and array from Validators? I'm worried that a user might try to pass them in the constructor, or pass them in as a combined validator. Should these functions be used as a regular validator at this point? |
||
|
|
||
| /** @internal */ | ||
| _setParentForControls(): void { | ||
| this.controls.forEach((control) => { control.setParent(this); }); | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just making a Note: When async validators are added this will need to get more complicated. Probably something like: