This repository was archived by the owner on Apr 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 27.4k
feat(form): $submitted state #8056
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 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 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 |
---|---|---|
|
@@ -404,6 +404,9 @@ describe('form', function() { | |
|
||
child.$setDirty(); | ||
expect(parent.$dirty).toBeTruthy(); | ||
|
||
child.$setSubmitted(); | ||
expect(parent.$submitted).toBeTruthy(); | ||
}); | ||
|
||
|
||
|
@@ -681,14 +684,42 @@ describe('form', function() { | |
expect(nestedInputCtrl.$dirty).toBe(false); | ||
}); | ||
}); | ||
|
||
describe('$setSubmitted', function() { | ||
beforeEach(function() { | ||
doc = $compile( | ||
'<form name="form" ng-submit="submitted = true">' + | ||
'<input type="text" ng-model="name" required />' + | ||
'<input type="submit" />' + | ||
'</form>')(scope); | ||
|
||
scope.$digest(); | ||
}); | ||
|
||
it('should not init in submitted state', function() { | ||
expect(scope.form.$submitted).toBe(false); | ||
}); | ||
|
||
it('should be in submitted state when submitted', function() { | ||
browserTrigger(doc, 'submit'); | ||
expect(scope.form.$submitted).toBe(true); | ||
}); | ||
|
||
it('should revert submitted back to false when $setPristine is called on the form', function() { | ||
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. what about 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. @lucassus care to explain a little bit what you meant? Sorry I didn't get it. |
||
scope.form.$submitted = true; | ||
scope.form.$setPristine(); | ||
expect(scope.form.$submitted).toBe(false); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('form animations', function() { | ||
beforeEach(module('ngAnimateMock')); | ||
|
||
function assertValidAnimation(animation, event, className) { | ||
function assertValidAnimation(animation, event, classNameAdded, classNameRemoved) { | ||
expect(animation.event).toBe(event); | ||
expect(animation.args[1]).toBe(className); | ||
expect(animation.args[1]).toBe(classNameAdded); | ||
expect(animation.args[2]).toBe(classNameRemoved); | ||
} | ||
|
||
var doc, scope, form; | ||
|
@@ -741,8 +772,7 @@ describe('form animations', function() { | |
|
||
form.$setPristine(); | ||
|
||
assertValidAnimation($animate.queue[0], 'removeClass', 'ng-dirty'); | ||
assertValidAnimation($animate.queue[1], 'addClass', 'ng-pristine'); | ||
assertValidAnimation($animate.queue[0], 'setClass', 'ng-pristine', 'ng-dirty ng-submitted'); | ||
})); | ||
|
||
it('should trigger custom errors as addClass/removeClass when invalid/valid', inject(function($animate) { | ||
|
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.
RFI: What's the rationale behind it? Why submitting a nested form needs marking all its parent as submitted?
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.
Good question, I kept the logic of how the other states propagates upwards.
Do you have an use case that goes against this?
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.
Yep, I have a multi-step form (aka wizard). I use .ng-submitted to style invalid inputs and have a plugin that detects if forms is dirty.
On pre-exit a state:
On entering a sub-state / nested form:
Style:
The border keeps being draw because there's a form styled with .ng-submitted further up. And I actually use $setPristine() because it clears $submitted, but then it breaks form dirty-checking plugin.
Some strategies that could it (if they existed):
I'm not sure yet about the invariants that could break with that. WDYT?
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.
A workaround while form.$setSubmitted(state: boolean) doesn't exist: