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

feat(ngMessages): add support for default message #16587

Merged
merged 4 commits into from
Jun 6, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
18 changes: 11 additions & 7 deletions src/ngMessages/messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,8 @@ var jqLite;
* <div ng-message-default>This field has an input error</div>
* </div>
* ```
*

*/
angular.module('ngMessages', [], function initAngularHelpers() {
// Access helpers from AngularJS core.
Expand Down Expand Up @@ -436,16 +438,18 @@ angular.module('ngMessages', [], function initAngularHelpers() {
messageCtrl.detach();
});

if (unmatchedMessages.length !== totalMessages) {
// Unset default message if set
if (ctrl.default) ctrl.default.detach();
var messageMatched = unmatchedMessages.length !== totalMessages;
var attachDefault = ctrl.default && !messageMatched && truthyKeys > 0;

if (attachDefault) {
ctrl.default.attach();
} else if (ctrl.default) {
ctrl.default.detach();
}

if (messageMatched || attachDefault) {
$animate.setClass($element, ACTIVE_CLASS, INACTIVE_CLASS);
} else {

// Set default message if keys in collection do not match any message
if (ctrl.default && truthyKeys > 0) ctrl.default.attach();

$animate.setClass($element, INACTIVE_CLASS, ACTIVE_CLASS);
Copy link
Contributor

Choose a reason for hiding this comment

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

This line should only be called if there is no default... Or perhaps this whole block should look like:

var messageMatched = unmatchedMessages.length !== totalMessages;

if (ctrl.default) {
  if (messageMatched) {
    ctrl.default.detach();
  } else if (truthyKeys > 0) {
    ctrl.default.attach();
  }
}
if (messageMatched || ctrl.default) {
  $animate.setClass($element, ACTIVE_CLASS, INACTIVE_CLASS);
} else {
  $animate.setClass($element, INACTIVE_CLASS, ACTIVE_CLASS);
}

}
};
Expand Down
12 changes: 11 additions & 1 deletion test/ngMessages/messagesSpec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

describe('ngMessages', function() {
fdescribe('ngMessages', function() {
Copy link
Contributor

Choose a reason for hiding this comment

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

Oops!

beforeEach(inject.strictDi());
beforeEach(module('ngMessages'));

Expand Down Expand Up @@ -674,18 +674,28 @@ describe('ngMessages', function() {
$rootScope.$digest();

expect(element.text().trim()).toBe('');
expect(element).not.toHaveClass('ng-active');

$rootScope.$apply(function() {
$rootScope.col = { unexpected: true };
});

expect(element.text().trim()).toBe('Default message is set');
expect(element).toHaveClass('ng-active');

$rootScope.$apply(function() {
$rootScope.col = { unexpected: false };
});

expect(element.text().trim()).toBe('');
expect(element).not.toHaveClass('ng-active');

$rootScope.$apply(function() {
$rootScope.col = { val: true, unexpected: true };
});

expect(element.text().trim()).toBe('Message is set');
expect(element).toHaveClass('ng-active');
}));

it('should not render a default message with ng-messages-multiple if another error matches',
Expand Down