Commit graph

269 commits

Author SHA1 Message Date
Dylan Hunn
c8e4d62882 refactor(forms): Split up model.ts. (#45217)
model.ts is currently extremely large. This is the first step in an attempt to refactor it to be more easily navigable and reviewable. This commit breaks up `model.ts` into the following new files:

* `model/abstract_model.ts`: The remainder of the model, including the `AbstractControl` base class and helper functions which are used throughout.
* `model/form_control.ts`: `FormControl`, `FormControlOptions`, and helpers, plus the constructor and untyped friends.
* `model/form_array.ts`: `FormArray` and untyped friends.
* `model/form_group.ts`: `FormGroup` and untyped friends.

This first phase is a purely mechanical code move. There is no new code at all, and no interfaces have been separated.

PR Close #45217
2022-03-01 19:49:31 +00:00
Dylan Hunn
bd12f9eda4 refactor(forms): Move getRawValue into the AbstractControl hierarchy. (#45200)
Currently, there is a freestanding `getRawValue` function which examines the type of the control. This is an issue for refactoring `model.ts` because it creates unnecessary dependencies between the `AbstractControl` classes. It is cleaner to simply add this method to the model hierarchy and call it directly, and will ease upcoming refactoring.

PR Close #45200
2022-03-01 01:07:12 +00:00
Dylan Hunn
b817f3cbf5 Revert "refactor(forms): Move getRawValue into the AbstractControl hierarchy. (#45200)" (#45219)
This reverts commit dfc4301b33.

PR Close #45219
2022-03-01 00:07:39 +00:00
Dylan Hunn
dfc4301b33 refactor(forms): Move getRawValue into the AbstractControl hierarchy. (#45200)
Currently, there is a freestanding `getRawValue` function which examines the type of the control. This is an issue for refactoring `model.ts` because it creates unnecessary dependencies between the `AbstractControl` classes. It is cleaner to simply add this method to the model hierarchy and call it directly, and will ease upcoming refactoring.

PR Close #45200
2022-02-28 19:44:07 +00:00
Dylan Hunn
7ee121f595 feat(forms): Add untyped versions of the model classes for use in migration. (#45205)
We had previously introduced an `AnyForUntypedForms` type alias. However, given our updated migration plan, we actually want to use aliases for the model classes themselves. This commit introduces these aliases, and adds them to the public API. It must be merged before the types, in order to migrate google3.

PR Close #45205
2022-02-28 19:43:22 +00:00
Dylan Hunn
4c80ca3700 refactor(forms): Clean up the find function. (#45199)
Move `find` into the `AbstractControl` hierarchy, and clean up a longstanding implementation todo.

PR Close #45199
2022-02-28 17:26:31 +00:00
Amer Yousuf
3d464c9995 docs(forms): improve email validator directive docs (#45014)
PR Close #45014
2022-02-08 09:57:57 -08:00
Kristiyan Kostadinov
ebf2fc5224 fix(forms): incorrectly keeping track of ngModel with ngFor inside a form (#40459)
When an `NgModel` is created within a `form`, it receives an `NgControl` based on its `name`, but
the control doesn't get swapped out if the name changes. This can lead to problems if the `NgModel`
is part of an `ngFor`, because the name can change based on its position in the list and a new
control can be defined with the same name, leading us to having multiple directives pointing to
the same control. For example, if we start off with a list like :

```
[0, 1, 2]; -> [NgModel(0), NgModel(1), NgModel(2)]
```

Then we remove the second item:

```
[0, 2]; -> [NgModel(0), NgModel(2)]
```

And finally, if we decide to add an item to the end of the list, we'll already have a control for
index 2, causing the list to look like:

```
[0, 2, 3]; -> [NgModel(0), NgModel(2), NgModel(2)]
```

These changes fix the issue by removing the old control when the `name` of the directive changes.

Fixes #38465.
Fixes #37920.

PR Close #40459
2022-02-07 13:16:10 -08:00
iRealNirmal
70e29be328 refactor(forms): update PatternValidator to inherit AbstractValidatorDirective (#44887)
This commit updates the `PatternValidator` class to inherit `AbstractValidatorDirective` to make it conistent with other validators.

Closes angular#42267

PR Close #44887
2022-02-07 12:07:34 -08:00
Ruslan Lekhman
d5719c2e0f fix(core): input coercion (#42803)
BREAKING CHANGE: Forms [email] input coercion

Forms [email] input value will be considered as true if it is defined with any value rather
than false and 'false'.

PR Close #42803
2022-02-07 10:52:59 -08:00
Sergej Grilborzer
8dd3f82f94 fix(forms): Correct empty validator to handle objects with a property length: 0. (#33729)
Form required validator should not reject objects that contain a length attribute set to zero.

Fixes #30718.

Co-authored-by: Dylan Hunn <dylhunn@gmail.com>

BREAKING CHANGE: objects with a length key set to zero will no longer validate as empty.

This is technically a breaking change, since objects with a key `length` and value `0` will no longer validate as empty. This is a very minor change, and any reliance on this behavior is probably a bug anyway.

PR Close #33729
2022-02-03 23:15:27 -08:00
Dylan Hunn
f0cfa00a34 refactor(forms): Move FormControl to an overridden exported constructor. (#44316) (#44806)
This implementation change was originally proposed as part of Typed Forms, and will have major consequences for that project as described in the design doc. Submitting it separately will greatly simplify the risk of landing Typed Forms. This change should have no visible impact on normal users of FormControl.

See the Typed Forms design doc here: https://docs.google.com/document/d/1cWuBE-oo5WLtwkLFxbNTiaVQGNk8ipgbekZcKBeyxxo.

PR Close #44316

PR Close #44806
2022-01-31 22:48:23 +00:00
arturovt
889de8276d fix(forms): ensure OnPush ancestors are marked dirty when the promise resolves (#44886)
Currently, `ngModel` calls` setValue` after the `resolvedPromise` is resolved.
The promise is resolved _after_ the child template executes. The change detection
is run but `OnPush` views are not updated because they are not marked as dirty.

PR Close #44886
2022-01-31 21:38:39 +00:00
Dylan Hunn
f490c2de4e feat(forms): support negative indices in FormArray methods. (#44848)
This new feature allows negative indices to wrap around from the back, just like ES2021 `Array.at`. In particular, the following methods accept negative indices, and behave like corresponding Array methods:
* `FormArray.at(index)`: behaves the same as `Array.at(index)`
* `FormArray.insert(index, control)`: behaves the same as `Array.splice(index, 0, control)`
* `FormArray.setControl(index, control)`: behaves the same as `Array.splice(index, 1, control)`
* `FormArray.removeAt(index, control)`: behaves the same as `Array.splice(index, 1)`

Previous work in #44746 and #44631 (by @amitbeck).

Issue #44642.

Co-authored-by: Amit Beckenstein <amitbeck@gmail.com>

PR Close #44848
2022-01-28 16:58:29 +00:00
Theodore Brown
f1a51457f9 refactor(forms): remove dead code (#37614)
This functionally dead code was originally introduced via pull request
were added to verify the fix, and the many comments on that issue after
it was closed indicate that it wasn't actually resolved.

In fact, setting `selectedIndex` does absolutely nothing here, since
the selected index is immediately overridden by setting the `value`
property. A working fix (with tests) for the IE/Safari bug is in pull
request #23784. Originally this dead code was removed as part of that PR,
but @AndrewKushnir recommended creating a separate PR for the cleanup.

PR Close #37614
2022-01-28 16:07:11 +00:00
iRealNirmal
531d1cf9a3 refactor(forms): update required validator and checkbox validator to inherit abstractValidator (#44162)
Modified required validator and checkbox validator to inherit abstractValidator.

For every validato type different PR will be raised as discussed in #42378.

Closes #42267

PR Close #44162
2022-01-24 14:50:57 -08:00
Dylan Hunn
fb27867ab8 Revert "refactor(forms): Move FormControl to an overridden exported constructor. (#44316)" (#44750)
This reverts commit cd5200ea68.

PR Close #44750
2022-01-18 19:54:51 -08:00
Dylan Hunn
41b8ecc9b8 refactor(forms): Make a couple small cleanups encountered while rebasing typed forms. (#44748)
These should all be no-ops publicly; they are just small issues encountered during a large rebase to bring typed-forms current.

PR Close #44748
2022-01-18 16:20:21 -08:00
Dylan Hunn
cd5200ea68 refactor(forms): Move FormControl to an overridden exported constructor. (#44316)
This implementation change was originally proposed as part of Typed Forms, and will have major consequences for that project as described in the design doc. Submitting it separately will greatly simplify the risk of landing Typed Forms. This change should have no visible impact on normal users of FormControl.

See the Typed Forms design doc here: https://docs.google.com/document/d/1cWuBE-oo5WLtwkLFxbNTiaVQGNk8ipgbekZcKBeyxxo.

PR Close #44316
2022-01-18 14:51:53 -08:00
iRealNirmal
c7b210d484 refactor(forms): update email validator to inherit abstractValidator (#44545)
Modified email validator to inherit abstractValidator.

For every validato type different PR will be raised as discussed in #42378.

Closes #42267

PR Close #44545
2022-01-12 20:41:40 +00:00
Andrew Kushnir
67df935b41 refactor(forms): use shared RuntimeError class (#44398)
This commit performs some refactoring of the AbstractControl-based classes to employ shared `RuntimeError` class and also updates the code to avoid duplication and improve minification.

PR Close #44398
2022-01-06 23:43:19 +00:00
Andrew Kushnir
8388151f4d refactor(forms): make validators code compatible with property renaming (#44500)
This commit refactors the code of the base validators class to make it compatible with the property renaming optimization. Currently the code makes an assumption that the field with a specified name (defined as a string) can be found on an object, but with property renaming optimization this is not correct.

PR Close #44500
2022-01-05 18:43:36 +00:00
Dylan Hunn
5ae1030462 feat(forms): Add an unused symbol AnyForUntypedForms. (#44467)
This symbol will support the typed forms migration in google3. The docs will be updated once the migration begins.

Pertains to #13721.

PR Close #44467
2022-01-04 12:18:26 -08:00
Dylan Hunn
6a0dadf46f fix(forms): _reduceValue arrow function now has correct types. (#44483)
I previously strengthened some weak types in #44370. One of these fixes exposed an incorrect call into `_reduceChildren` from `_reduceValue`. This was caught in google3 by a caller who was extending `FormGroup` and overriding these methods.

Special thanks to Bart G for catching this issue and suggesting a fix.

PR Close #44483
2021-12-15 12:27:06 -05:00
Dylan Hunn
72092ebd26 feat(forms): Allow a FormControl to use initial value as default. (#44434)
Allow a FormControl to be reset to its initial value. Provide this feature via a new option in a FormControlOptions interface, based on AbstractControlOptions.

Also, expose the default value as part of the public API. This is part of a feature that has been requested elsewhere (e.g. in #19747).

This was originally proposed as part of typed forms. As discussed in the GDE session (and after with akushnir/alxhub), it is likely better to just reuse the initial value rather than accepting an additional default.

It is desirable to land this separately in order to reduce the scope of the typed forms PR, and make it a types-only change.

Pertains to issue #13721.

PR Close #44434
2021-12-15 10:36:13 -05:00
Dylan Hunn
815d4ceeef fix(forms): I indroduced a minor error in a previous PR: pendingValue is a value not a boolean flag. (#44450)
The bug should have no effect since it's a typings-only, internal-only bug, but it's good to fix nonetheless.

PR Close #44450
2021-12-14 16:09:42 -05:00
Dylan Hunn
7df9127088 refactor(forms): Fix weak helper types in the Forms package, mostly Function types that can be strengthened to specific callbacks. This was originally proposed as part of Typed Forms, but it may be possible to submit at least some of these changes separately and unconditionally (i.e. without relying on TypedOrUntyped.) (#44370)
It is desirable to land this separately to reduce the scope of the Typed Forms PR, by focusing it only on the new type parameters (rather than incidental strictness fixes).

PR Close #44370
2021-12-10 17:13:24 -05:00
Alex Rickabaugh
0cd524b148 Revert "refactor(forms): Move FormControl to an overridden exported constructor. (#44316)"
This reverts commit cdf50ff931.

Reverting as this needs a little more work on the documentation side, plus
the `export declare interface` syntax in `model.ts` might have unintended
side effects in g3.
2021-12-08 13:36:58 -05:00
Dylan Hunn
cdf50ff931 refactor(forms): Move FormControl to an overridden exported constructor. (#44316)
This implementation change was originally proposed as part of Typed Forms, and will have major consequences for that project as described in the design doc. Submitting it separately will greatly simplify the risk of landing Typed Forms. This change should have no visible impact on normal users of FormControl.

See the Typed Forms design doc here: https://docs.google.com/document/d/1cWuBE-oo5WLtwkLFxbNTiaVQGNk8ipgbekZcKBeyxxo.

PR Close #44316
2021-12-08 13:24:28 -05:00
Andrew Kushnir
3f6d8f38de refactor(forms): minor refactoring of SelectMultipleControlValueAccessor (#44261)
This commit updates the code of the `SelectMultipleControlValueAccessor` to:
- improve typings to make them more precise
- updates the note that refers to IE, but we still can not remove the branch since it's needed for Universal (that uses Domino)

PR Close #44261
2021-11-30 16:43:11 -05:00
Ramesh Thiruchelvam
24baa5e61d docs(forms): add an example for async validator token (#44301)
to be consistent with the validator token, have the example code for the async validator token.

PR Close #44301
2021-11-30 14:24:26 -05:00
Ruslan Usmanov
ca5b9b558c refactor(forms): eagerly initialize data members (#44292)
Data members in AbstractControl should be eagerly
initialized to address issue/24571. This eliminates the need to
constantly check for truthiness and makes code much more readable.

More PRs to follow to address issue/24571.

PR Close #44292
2021-11-30 14:15:16 -05:00
Andrew Kushnir
38c02e13a5 fix(forms): make the FormControlStatus available as a public API (#44183)
This commit makes the `FormControlStatus` symbol available as a public API. The symbol itself
was intended to become a part of the public API, but due to the missing re-export, the symbol
remains private.

Fixes #44176.

PR Close #44183
2021-11-17 10:37:28 -08:00
Andrew Kushnir
65597d650b refactor(forms): inherit ngOnChanges hooks from the base class (#43945)
This commit updates the code of the min/max and minlength/maxlength validator directives to inherit `ngOnChanges` hooks from the base class (the `AbstractValidatorDirective` one), rather than implementing the hooks on the child classes. This was needed to avoid issues with hooks inheritance in ViewEngine, but since it's deprecated, the code can be cleaned up.

PR Close #43945
2021-11-17 10:36:40 -08:00
Roman Frołow
9f3791432c docs(forms): fix typo in toInteger() function (#44075)
PR Close #44075
2021-11-08 16:59:08 +00:00
iRealNirmal
1543aaedd6 refactor(forms): update minlength/maxlength to inherit abstractValidator (#43998)
Modified minlength and maxlength validator to inherit abstractValidator

For every validator type different PR will be raised as discussed in #42378.

Closes #42267

PR Close #43998
2021-11-05 20:09:53 +00:00
Jessica Janiuk
17be47e781 Revert "refactor(forms): update minlength/maxlength to inherit abstractValidator" (#43971)
This reverts commit 7b31b887f9.

PR Close #43971
2021-10-27 21:42:26 +00:00
iRealNirmal
5c22dfe861 refactor(forms): update minlength/maxlength to inherit abstractValidator (#43835)
Modified minlength and maxlength validator to inherit abstractValidator

For every validator type different PR will be raised as discussed in #42378.

Closes #42267

PR Close #43835
2021-10-26 20:24:40 +00:00
iRealNirmal
d9d8f950e9 feat(forms): allow disabling min/max validators dynamically (by setting the value to null) (#42978)
This commit updates the logic of the `min` and `max` validators to allow
disabling them dynamically in case `null` is provided as a value. For example: `<input
type="number" [min]="minValue">`, when `minValue` might be set to `null` in a
component class. This should allow `min` and `max` validators to be used for dynamic forms.

Note: similar support was added to the `minLength` and `maxLength`
validators earlier (see #42565).

PR Close #42978
2021-09-24 10:35:31 -07:00
Kristiyan Kostadinov
c3968047b6 refactor(forms): make error messages easier to tree shake (#43223)
Currently the error message functions are defined as static methods on a class which means that as soon as one of them is used somewhere, all of them have to be retained. This isn't a problem at the moment, because all of them are behind `ngDevMode` checks, but it's error prone and it's easy to fix.

These changes move them out into functions so that they can be imported individually. It also has the advantage of allowing Webpack to minify the function names.

PR Close #43223
2021-08-26 10:05:36 -07:00
Dylan Hunn
e49fc96ed3 feat(forms): Make Form Statuses use stricter types. (#42952)
Specifically: narrow the type used for form statuses from string to a union of possible statuses. Change the API methods from any to use the new type.

This is a breaking change. However, as discussed in the PR, breakage seems minimal, and google3 has been prepped to land this.

Background: we uncovered these any typings in the course of design work for typed forms. They could be fixed in a non-breaking manner by piggybacking them on top of the new typed forms generics, but it would be much cleaner to fix them separately if possible.

BREAKING CHANGE:

A new type called `FormControlStatus` has been introduced, which is a union of all possible status strings for form controls. `AbstractControl.status` has been narrowed from `string` to `FormControlStatus`, and `statusChanges` has been narrowed from `Observable<any>` to `Observable<FormControlStatus>`. Most applications should consume the new types seamlessly. Any breakage caused by this change is likely due to one of the following two problems: (1) the app is comparing `AbstractControl.status` against a string which is not a valid status; or, (2) the app is using `statusChanges` events as if they were something other than strings.

PR Close #42952
2021-08-04 16:28:20 -07:00
Dylan Hunn
1d9d02696e feat(forms): add hasValidators, addValidators, and removeValidators methods (for both sync and async) (#42838)
Several new functionalities are possible with this change: the most requested is that callers can now check whether a control has a required validator. Other uses include incrementally changing the validators set without doing an expensive operation to reset all validators.

Closes #13461.

PR Close #42838
2021-07-22 16:31:00 +00:00
iRealNirmal
a502279592 feat(forms): allow minLength/maxLength validator to be bound to null (#42565)
If the validator is bound to be `null` then no validation occurs and
attribute is not added to DOM.

For every validator type different PR will be raised as discussed in
https://github.com/angular/angular/pull/42378.

Closes #42267.

PR Close #42565
2021-07-21 22:35:59 +00:00
Paul Gschwendtner
b5ab7aff43 refactor: add override keyword to members implementing abstract declarations (#42512)
In combination with the TS `noImplicitOverride` compatibility changes,
we also want to follow the best-practice of adding `override` to
members which are implemented as part of abstract classes. This
commit fixes all instances which will be flagged as part of the
custom `no-implicit-override-abstract` TSLint rule.

PR Close #42512
2021-07-12 13:11:17 -07:00
Paul Gschwendtner
01e869a45b refactor(forms): ensure compatibility with noImplicitOverride (#42512)
Adds the `override` keyword to the `forms` sources to ensure
compatibility with `noImplicitOverride`.

PR Close #42512
2021-07-12 13:11:16 -07:00
David Gilson
ad08b17b05 docs(forms): add diff between add and set control (#42638)
PR Close #42638
2021-07-08 10:27:43 -07:00
Marius Bethge
f3a79878af docs(forms): correct sample code for FormArray.reset (#42477)
Remove unexpected this, correct output comment for arr.value and correct parameter type for FormArray.get().

PR Close #42477
2021-06-16 14:01:55 -07:00
Dylan Hunn
7180ec9e7c fix(forms): changes to status not always being emitted to statusChanges observable for async validators. (#42553)
When a FormControl, FormArray, or FormGroup is first constructed, if an async validator is attached, the `statusChanges` observable should receive a message when the validator complete (i.e. pending -> valid/invalid). If the validator was provided as part of the constructor options, it was not fired at construction time, which is fixed in this PR.

Fixes #35309.

PR Close #42553
2021-06-14 09:57:57 -07:00
Dylan Hunn
34ce635e3a feat(forms): undo revert and add ng-submitted class to forms that have been submitted. (#42132) (#42132)
As previously discussed in pull/31070 and issues/30486, this would be useful because it is often desirable to apply styles to fields that are both `ng-invalid` and `ng-pristine` after the first attempt at form submission, but Angular does not provide any simple way to do this (although evidently Angularjs did). This will now be possible with a descendant selector such as `.ng-submitted .ng-invalid`.

In this implementation, the directive that sets control status classes on forms and formGroups has its set of statuses widened to include `ng-submitted`. Then, in the event that `is('submitted')` is invoked, the `submitted` property of the control container is returned iff it exists. This is preferred over checking whether the container is a `Form` or `FormGroup` directly to avoid reflecting on those classes.

Closes #30486.

PR Close #42132.

This reverts commit 00b1444d12, undoing the rollback of this change.

PR Close #42132
2021-06-08 14:02:29 -07:00
Paul Gschwendtner
2d0ff0a5d3 ci: add lint error for files with missing trailing new-line (#42478)
For quite a while it is an unspoken convention to add a trailing
new-line files within the Angular repository. This was never enforced
automatically, but has been frequently raised in pull requests through
manual review. This commit sets up a lint rule so that this is
"officially" enforced and doesn't require manual review.

PR Close #42478
2021-06-04 13:31:03 -07:00