Skip to content

feat(forms): add ng-submitted class to forms that have been submitted.#42132

Closed
dylhunn wants to merge 1 commit intoangular:masterfrom
dylhunn:ng-submitted-class
Closed

feat(forms): add ng-submitted class to forms that have been submitted.#42132
dylhunn wants to merge 1 commit intoangular:masterfrom
dylhunn:ng-submitted-class

Conversation

@dylhunn
Copy link
Contributor

@dylhunn dylhunn commented May 17, 2021

PR Checklist

Please check if your PR fulfills the following requirements:

PR Type

What kind of change does this PR introduce?

  • Bugfix
  • Feature
  • Code style update (formatting, local variables)
  • Refactoring (no functional changes, no api changes)
  • Build related changes
  • CI related changes
  • Documentation content changes
  • angular.io application / infrastructure changes
  • Other... Please describe:

What is the current behavior?

Issue Number: 30486

Currently, no status class is set on a form to show that it has been submitted. 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 currently provide any simple way to do this (although evidently Angularjs did).

What is the new behavior?

A new ng-submitted class is set on submitted forms. It will now be possible to select untouched but invalid fields in a submitted form (for example, with a 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.

Does this PR introduce a breaking change?

  • Yes
  • No

Other information

Closes #30486.
Closes #31070.

@google-cla google-cla bot added the cla: yes label May 17, 2021
@dylhunn dylhunn requested a review from AndrewKushnir May 17, 2021 17:20
@dylhunn dylhunn marked this pull request as ready for review May 17, 2021 17:21
@ngbot ngbot bot added this to the Backlog milestone May 17, 2021
@dylhunn dylhunn added action: review The PR is still awaiting reviews from at least one requested reviewer target: minor This PR is targeted for the next minor release labels May 17, 2021
@dylhunn dylhunn force-pushed the ng-submitted-class branch 3 times, most recently from 1c9e212 to b39597c Compare May 17, 2021 20:07
Copy link
Contributor

@AndrewKushnir AndrewKushnir left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dylhunn great start, the changes are in the right direction 👍

There are few comments that I've left + I think we'd need to add more tests (to verify the fact that only the top-level form groups receive ng-submitted class). I will write a separate comment related to tests and just want to share the rest of the feedback, so you can have a look at it meanwhile.

Thank you.

@dylhunn dylhunn force-pushed the ng-submitted-class branch from b39597c to 3f72c22 Compare May 17, 2021 21:07
@AndrewKushnir
Copy link
Contributor

@dylhunn just wanted to share some test cases that I think would be great to have. Some of them after already fully covered (for ex. "Template-driven Forms" -> Case 2), I listed the cases anyways just to have a full picture (for myself, so I'm not missing anything). I also mentioned what we can look for in the tests, to make sure we do not assign extra classes to elements that are not expected to have that class.

Reactive Forms

1. Simple case:

<form [formGroup]="group">
 <input [formControl]="control">
</form>

We can check that:

  • <form> has ng-submitted class after submission and has no class after reset
  • <input> has no ng-submitted class before submission and after reset

2. With nested formGroups:

<form [formGroup]="group">
   <div [formGroup]="nestedGroup">
     <input [formControl]="control">
   </div>
</form>

We can check that:

  • <form> has ng-submitted class after submission and has no class after reset
  • <div> has no ng-submitted class before submission and after reset
  • <input> has no ng-submitted class before submission and after reset

3. With nested formArrayNames:

<form [formGroup]="group">
   <div [formArrayName]="name">
     <input [formControl]="control">
   </div>
</form>

We can check that:

  • <form> has ng-submitted class after submission and has no class after reset
  • <div> has no ng-submitted class before submission and after reset
  • <input> has no ng-submitted class before submission and after reset

4. Similar to a test case above, but with nested formGroupNames:

<form [formGroup]="group">
   <div [formGroupName]="name">
     <input [formControl]="control">
   </div>
</form>

We can check that:

  • <form> has ng-submitted class after submission and has no class after reset
  • <div> has no ng-submitted class before submission and after reset
  • <input> has no ng-submitted class before submission and after reset

Template-driven Forms

1. Simple case:

<form>
 <input [(ngModel)]="control">
</form>

We can check that:

  • <form> has ng-submitted class after submission and has no class after reset
  • <input> has no ng-submitted class before submission and after reset

2. With nested ngModelGroups:

<form>
  <div ngModelGroup="group">
    <input [(ngModel)]="control">
  </div>
</form>

We can check that:

  • <form> has ng-submitted class after submission and has no class after reset
  • <div> has no ng-submitted class before submission and after reset
  • <input> has no ng-submitted class before submission and after reset

@AndrewKushnir
Copy link
Contributor

@dylhunn quick note: we should also update the relevant section of the aio/content/guide/forms.md file to include information on the ng-submitted class.

@dylhunn dylhunn force-pushed the ng-submitted-class branch from 3f72c22 to 9f29443 Compare May 19, 2021 19:37
Copy link
Contributor

@AndrewKushnir AndrewKushnir left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The changes look good, thanks @dylhunn 👍

Just left a few comments related to tests.

@AndrewKushnir AndrewKushnir added action: presubmit The PR is in need of a google3 presubmit and removed action: review The PR is still awaiting reviews from at least one requested reviewer labels May 19, 2021
@pullapprove pullapprove bot requested a review from atscott May 19, 2021 23:59
@dylhunn dylhunn removed request for alxhub and jelbourn May 20, 2021 16:22
@pullapprove pullapprove bot requested review from alxhub and jelbourn May 20, 2021 16:22
@dylhunn dylhunn force-pushed the ng-submitted-class branch from 9f29443 to c5a7109 Compare May 20, 2021 17:33
@dylhunn dylhunn force-pushed the ng-submitted-class branch from 415cfb9 to f024d75 Compare May 25, 2021 19:27
@dylhunn
Copy link
Contributor Author

dylhunn commented May 25, 2021

Merging is currently blocked on a g3 presubmit issue, as described internally in b/189261737.

@dylhunn
Copy link
Contributor Author

dylhunn commented Jun 2, 2021

Some updates:

  • Real google3 breakage is fixed (see b/189261737 and cl/377141465)
  • Some targets still fail to build in the presubmit; however these breakages are present in a clean client and can be ignored

@dylhunn dylhunn added action: merge The PR is ready for merge by the caretaker and removed action: presubmit The PR is in need of a google3 presubmit labels Jun 2, 2021
@jessicajaniuk jessicajaniuk reopened this Jun 4, 2021
@jessicajaniuk
Copy link
Contributor

This PR caused breakages in google3 and was rolled back. We'll need to run a TGP with these changes to verify there are no issues.

@AndrewKushnir AndrewKushnir added state: blocked and removed action: merge The PR is ready for merge by the caretaker labels Jun 4, 2021
@dylhunn dylhunn closed this Jun 7, 2021
@dylhunn dylhunn force-pushed the ng-submitted-class branch from f024d75 to cd22c7a Compare June 7, 2021 16:38
@dylhunn dylhunn reopened this Jun 7, 2021
@dylhunn dylhunn force-pushed the ng-submitted-class branch from efcc737 to 4f09d5a Compare June 7, 2021 17:12
@dylhunn dylhunn force-pushed the ng-submitted-class branch from 4f09d5a to eeb89d4 Compare June 8, 2021 00:08
…e been submitted. (angular#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 angular#30486.

PR Close angular#42132.

This reverts commit 00b1444, undoing the rollback of this change.
@dylhunn dylhunn force-pushed the ng-submitted-class branch from eeb89d4 to 163af75 Compare June 8, 2021 16:35
@dylhunn
Copy link
Contributor Author

dylhunn commented Jun 8, 2021

The issue seems to have been that ViewEngine chokes on the spread operator as used in the host binding. I'll investigate this more separately, but it should be fixed here for now.

@dylhunn
Copy link
Contributor Author

dylhunn commented Jun 8, 2021

After re-running flakes, TGP looks good (googler link).

@dylhunn dylhunn added action: merge The PR is ready for merge by the caretaker and removed state: blocked labels Jun 8, 2021
@jessicajaniuk jessicajaniuk added action: merge The PR is ready for merge by the caretaker and removed action: merge The PR is ready for merge by the caretaker labels Jun 8, 2021
'[class.ng-pending]': 'is("pending")',
};

export const ngGroupStatusHost = {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dylhunn could you please create a followup PR to add some information on why the content of the ngGroupStatusHost object partially duplicates the ngControlStatusHost one (related to spread operator support in ViewEngine and the need to refactor the code once ViewEngine is removed)?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will do.

@angular-automatic-lock-bot
Copy link

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Jul 9, 2021
@dylhunn dylhunn deleted the ng-submitted-class branch December 14, 2021 23:07
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

action: merge The PR is ready for merge by the caretaker area: forms cla: yes target: minor This PR is targeted for the next minor release

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Apply ng-submitted class to formGroups that have been submitted

5 participants

Comments