Title: make it easier to access the submitted status of a form · Issue #48402 · angular/angular · GitHub
Open Graph Title: make it easier to access the submitted status of a form · Issue #48402 · angular/angular
X Title: make it easier to access the submitted status of a form · Issue #48402 · angular/angular
Description: Which @angular/* package(s) are relevant/related to the feature request? forms Description Several libraries (like ngx-valdemort or angular material) use the submitted property of the parent formGroupDirective in order to decide whether ...
Open Graph Description: Which @angular/* package(s) are relevant/related to the feature request? forms Description Several libraries (like ngx-valdemort or angular material) use the submitted property of the parent formGr...
X Description: Which @angular/* package(s) are relevant/related to the feature request? forms Description Several libraries (like ngx-valdemort or angular material) use the submitted property of the parent formGr...
Opengraph URL: https://github.com/angular/angular/issues/48402
X: @github
Domain: patch-diff.githubusercontent.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"make it easier to access the submitted status of a form","articleBody":"### Which @angular/* package(s) are relevant/related to the feature request?\r\n\r\nforms\r\n\r\n### Description\r\n\r\nSeveral libraries (like ngx-valdemort or angular material) use the `submitted` property of the parent formGroupDirective in order to decide whether or not to display error messages. This works fine as long as the form group directive is only used on the top-level form, and not for nested form groups.\r\n\r\nSee [this working example](https://stackblitz.com/edit/angular-zgmq7p?file=src%2Fapp%2Fform-field-error-example.ts,src%2Fapp%2Fform-field-error-example.html), using formGroupName for the nested form group and [this non-working example](https://stackblitz.com/edit/angular-zgmq7p-2ygua5?file=src%2Fapp%2Fform-field-error-example.ts,src%2Fapp%2Fform-field-error-example.html), using formGroup for the nested form group.\r\n\r\nOne might say that using formGroupName is an easy-enough alternative but\r\n- with typed forms, it's less type-safe\r\n- it is possible, but more complex, to use it from a child component, because it requires a parent formGroupDirective in the same template, unless a view provider is specified for ControlContainer, but that's far from easy to find out, and has its limitations. AFAIK, most people pass the FormGroup as input to the child component, which then uses the formGroup directive.\r\n\r\n### Proposed solution\r\n\r\nThere are several solutions I can see\r\n\r\n1. being able, from the formGroupDirective, to navigate to its parent, recursively, until getting to the top-level one which contains the correct submitted value\r\n2. Add some additional topLevelFormGroup directive, which could use the new directive composition API, and which would get a reference to the formGroupDirective on the same host to give access to it. This TopLevelFormGroupDirective could then be injected in place of the FormGroupDirective to get direct access to the submitted flag\r\n3. Have the formGroupDirective store the submitted flag in its form group, just like it's done for similar flages such as touched, dirty. Since FormGroups do have a reference to their parent, libraries needing to access the submitted property could easily navigate to the top-level directive\r\n4. ???\r\n\r\nThe third solution seems like the most logical to me. It has the additional advantage that it could allow `FormGroup.reset()` to also reset the submitted flag, just as it resets the touched flags.\r\n\r\n\r\n### Alternatives considered\r\n\r\nClearly document that formGroupDirective is only ever supposed to be used for the top-level form (some parts of the doc suggest that, like the documentation of the `path` property of FormGroupDirective, but on the other hands, I've seen many usages of the formGroupDirective in the wild for nested form groups), and clearly document how to best delegate the display of a nested form group to a child component, using `formGroupName`. The best solution I've found is to use the following in the parent component:\r\n\r\n```html\r\n\u003cdiv formGroupName=\"address\"\u003e\r\n \u003capp-address\u003e\u003c/app-address\u003e\r\n\u003c/div\u003e\r\n```\r\n\r\nand use this in the decorator of the child component to be able to access the parent form group name:\r\n\r\n```ts\r\n viewProviders: [{ provide: ControlContainer, useExisting: FormGroupName }]\r\n```\r\n\r\nBut this solution isn't without problems. For example: it forces every usage of address to be inside a formGroupName. It can't be used directly under a formGroupDirective. It's also not obvious at all.","author":{"url":"https://github.com/jnizet","@type":"Person","name":"jnizet"},"datePublished":"2022-12-08T13:16:18.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":4},"url":"https://github.com/48402/angular/issues/48402"}
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:764cd93b-0204-1d3a-d56d-2a5db39357a4 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | CCFC:31F73:388939:48D458:69757190 |
| html-safe-nonce | b20a922e1e85eecfcdcd3911e6a6ceddff5d4e3a58a4fae178d66dff84e6dfde |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDQ0ZDOjMxRjczOjM4ODkzOTo0OEQ0NTg6Njk3NTcxOTAiLCJ2aXNpdG9yX2lkIjoiMTMwNjIyMjEwNzgyMzU5OTUyIiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 3acac9a44b3dd0d82e1277f249af40d5ba32cbd4b282e586ff89f7d858523afe |
| hovercard-subject-tag | issue:1484621855 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/angular/angular/48402/issue_layout |
| twitter:image | https://opengraph.githubassets.com/fd55124e3d7c12bc8dee301fc44035f45009c8ae71b59221c1516908d96a6abc/angular/angular/issues/48402 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/fd55124e3d7c12bc8dee301fc44035f45009c8ae71b59221c1516908d96a6abc/angular/angular/issues/48402 |
| og:image:alt | Which @angular/* package(s) are relevant/related to the feature request? forms Description Several libraries (like ngx-valdemort or angular material) use the submitted property of the parent formGr... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | jnizet |
| hostname | github.com |
| expected-hostname | github.com |
| None | 4a4bf5f4e28041a9d2e5c107d7d20b78b4294ba261cab243b28167c16a623a1f |
| turbo-cache-control | no-preview |
| go-import | github.com/angular/angular git https://github.com/angular/angular.git |
| octolytics-dimension-user_id | 139426 |
| octolytics-dimension-user_login | angular |
| octolytics-dimension-repository_id | 24195339 |
| octolytics-dimension-repository_nwo | angular/angular |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 24195339 |
| octolytics-dimension-repository_network_root_nwo | angular/angular |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 488b30e96dfd057fbbe44c6665ccbc030b729dde |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width