Skip to content

Commit e268a05

Browse files
karabenlesh
authored andcommitted
docs(forms): update desc for hasError and getError (angular#27861)
This commit adds docs for the changes made in angular#20211. Closes angular#19734. PR Close angular#27861
1 parent 1f1e77b commit e268a05

2 files changed

Lines changed: 97 additions & 12 deletions

File tree

‎packages/forms/src/directives/abstract_control_directive.ts‎

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,31 @@ export abstract class AbstractControlDirective {
150150
/**
151151
* @description
152152
* Reports whether the control with the given path has the error specified.
153-
* If no path is given, it checks for the error on the present control.
153+
*
154+
* @param errorCode The code of the error to check
155+
* @param path A list of control names that designates how to move from the current control
156+
* to the control that should be queried for errors.
157+
*
158+
* @usageNotes
159+
* For example, for the following `FormGroup`:
160+
*
161+
* ```
162+
* form = new FormGroup({
163+
* address: new FormGroup({ street: new FormControl() })
164+
* });
165+
* ```
166+
*
167+
* The path to the 'street' control from the root form would be 'address' -> 'street'.
168+
*
169+
* It can be provided to this method in one of two formats:
170+
*
171+
* 1. An array of string control names, e.g. `['address', 'street']`
172+
* 1. A period-delimited list of control names in one string, e.g. `'address.street'`
173+
*
174+
* If no path is given, this method checks for the error on the current control.
175+
*
176+
* @returns whether the given error is present in the control at the given path.
177+
*
154178
* If the control is not present, false is returned.
155179
*/
156180
hasError(errorCode: string, path?: string[]): boolean {
@@ -160,7 +184,29 @@ export abstract class AbstractControlDirective {
160184
/**
161185
* @description
162186
* Reports error data for the control with the given path.
163-
* If the control is not present, null is returned.
187+
*
188+
* @param errorCode The code of the error to check
189+
* @param path A list of control names that designates how to move from the current control
190+
* to the control that should be queried for errors.
191+
*
192+
* @usageNotes
193+
* For example, for the following `FormGroup`:
194+
*
195+
* ```
196+
* form = new FormGroup({
197+
* address: new FormGroup({ street: new FormControl() })
198+
* });
199+
* ```
200+
*
201+
* The path to the 'street' control from the root form would be 'address' -> 'street'.
202+
*
203+
* It can be provided to this method in one of two formats:
204+
*
205+
* 1. An array of string control names, e.g. `['address', 'street']`
206+
* 1. A period-delimited list of control names in one string, e.g. `'address.street'`
207+
*
208+
* @returns error data for that particular error. If the control or error is not present,
209+
* null is returned.
164210
*/
165211
getError(errorCode: string, path?: string[]): any {
166212
return this.control ? this.control.getError(errorCode, path) : null;

‎packages/forms/src/model.ts‎

Lines changed: 49 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -645,27 +645,66 @@ export abstract class AbstractControl {
645645
get(path: Array<string|number>|string): AbstractControl|null { return _find(this, path, '.'); }
646646

647647
/**
648-
* Reports error data for a specific error occurring in this control or in another control.
648+
* @description
649+
* Reports error data for the control with the given path.
649650
*
650-
* @param errorCode The error code for which to retrieve data
651-
* @param path The path to a control to check. If not supplied, checks for the error in this
652-
* control.
651+
* @param errorCode The code of the error to check
652+
* @param path A list of control names that designates how to move from the current control
653+
* to the control that should be queried for errors.
654+
*
655+
* @usageNotes
656+
* For example, for the following `FormGroup`:
653657
*
654-
* @returns The error data if the control with the given path has the given error, otherwise null
655-
* or undefined.
658+
* ```
659+
* form = new FormGroup({
660+
* address: new FormGroup({ street: new FormControl() })
661+
* });
662+
* ```
663+
*
664+
* The path to the 'street' control from the root form would be 'address' -> 'street'.
665+
*
666+
* It can be provided to this method in one of two formats:
667+
*
668+
* 1. An array of string control names, e.g. `['address', 'street']`
669+
* 1. A period-delimited list of control names in one string, e.g. `'address.street'`
670+
*
671+
* @returns error data for that particular error. If the control or error is not present,
672+
* null is returned.
656673
*/
657674
getError(errorCode: string, path?: string[]): any {
658675
const control = path ? this.get(path) : this;
659676
return control && control.errors ? control.errors[errorCode] : null;
660677
}
661678

662679
/**
680+
* @description
663681
* Reports whether the control with the given path has the error specified.
664682
*
665-
* @param errorCode The error code for which to retrieve data
666-
* @param path The path to a control to check. If not supplied, checks for the error in this
667-
* control.
668-
* @returns True when the control with the given path has the error, otherwise false.
683+
* @param errorCode The code of the error to check
684+
* @param path A list of control names that designates how to move from the current control
685+
* to the control that should be queried for errors.
686+
*
687+
* @usageNotes
688+
* For example, for the following `FormGroup`:
689+
*
690+
* ```
691+
* form = new FormGroup({
692+
* address: new FormGroup({ street: new FormControl() })
693+
* });
694+
* ```
695+
*
696+
* The path to the 'street' control from the root form would be 'address' -> 'street'.
697+
*
698+
* It can be provided to this method in one of two formats:
699+
*
700+
* 1. An array of string control names, e.g. `['address', 'street']`
701+
* 1. A period-delimited list of control names in one string, e.g. `'address.street'`
702+
*
703+
* If no path is given, this method checks for the error on the current control.
704+
*
705+
* @returns whether the given error is present in the control at the given path.
706+
*
707+
* If the control is not present, false is returned.
669708
*/
670709
hasError(errorCode: string, path?: string[]): boolean { return !!this.getError(errorCode, path); }
671710

0 commit comments

Comments
 (0)