Skip to content
This repository has been archived by the owner on Apr 12, 2024. It is now read-only.

Form $attempted status #5574

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 30 additions & 4 deletions src/ng/directive/form.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
'use strict';

/* global -nullFormCtrl */
/* global
-nullFormCtrl,
-ATTEMPTED_CLASS
*/
var nullFormCtrl = {
$addControl: noop,
$removeControl: noop,
$setValidity: noop,
$setDirty: noop,
$setPristine: noop
};
$setPristine: noop,
$setAttempted: noop
},
ATTEMPTED_CLASS = 'ng-attempted';

/**
* @ngdoc object
Expand Down Expand Up @@ -60,6 +65,7 @@ function FormController(element, attrs) {
form.$pristine = true;
form.$valid = true;
form.$invalid = false;
form.$attempted = false;

parentForm.$addControl(form);

Expand Down Expand Up @@ -206,9 +212,22 @@ function FormController(element, attrs) {
control.$setPristine();
});
};

/**
* @ngdoc function
* @name ng.directive:form.FormController#$setAttempted
* @methodOf ng.directive:form.FormController
*
* @description
* Sets the form to its submission attempted state.
*/
form.$setAttempted = function (value) {
value ? element.addClass(ATTEMPTED_CLASS) : element.removeClass(ATTEMPTED_CLASS);
form.$attempted = value;
parentForm.$setAttempted(value);
};
}


/**
* @ngdoc directive
* @name ng.directive:ngForm
Expand Down Expand Up @@ -253,6 +272,7 @@ function FormController(element, attrs) {
* - `ng-invalid` Is set if the form is invalid.
* - `ng-pristine` Is set if the form is pristine.
* - `ng-dirty` Is set if the form is dirty.
* - `ng-attempted` Is set if the form was submitted in invalid state.
*
*
* # Submitting a form and preventing the default action
Expand Down Expand Up @@ -350,6 +370,12 @@ var formDirectiveFactory = function(isNgForm) {
removeEventListenerFn(formElement[0], 'submit', preventDefaultListener);
}, 0, false);
});

formElement.on('submit', function() {
scope.$apply(function() {
controller.$setAttempted(controller.$invalid);
});
});
}

var parentFormCtrl = formElement.parent().controller('form'),
Expand Down
24 changes: 24 additions & 0 deletions test/ng/directive/formSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,9 @@ describe('form', function() {

child.$setDirty();
expect(parent.$dirty).toBeTruthy();

child.$setAttempted(true);
expect(parent.$attempted).toBeTruthy();
});


Expand Down Expand Up @@ -593,4 +596,25 @@ describe('form', function() {
expect(nestedInputCtrl.$dirty).toBe(false);
});
});

describe('$setAttempted', 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 attempted state', function() {
expect(scope.form.$attempted).toBe(false);
});

it('should be in attempted state when invalid and submitted', function() {
browserTrigger(doc, 'submit');
expect(scope.form.$attempted).toBe(true);
});
});
});