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

Commit

Permalink
fix(Directive): Fixed fallback functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
hafstad committed Jul 19, 2016
1 parent feb1718 commit 19a59db
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 21 deletions.
22 changes: 14 additions & 8 deletions dist/component.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/component.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/component.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/component.min.js.map

Large diffs are not rendered by default.

23 changes: 16 additions & 7 deletions src/nReplaceWithValidation.directive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,9 @@ namespace nReplaceWithValidation {
private messages: any;
private input: any;

static $inject: Array<string> = ['$sce', 'nReplaceWithValidationConfig'];
constructor(private $sce: any,
static $inject: Array<string> = ['$scope', '$sce', 'nReplaceWithValidationConfig'];
constructor(private $scope: any,
private $sce: any,
private nReplaceWithValidationConfig: INReplaceWithValidationProvider) {

// Extend messages with config (defaults)
Expand All @@ -58,11 +59,19 @@ namespace nReplaceWithValidation {
this.messages[message] = this.$sce.trustAsHtml(this.messages[message]);
}

}

private useFallback(): boolean {
if(this.input.$pristine && !this.input.$$parentForm.$submitted) return false;
return !this.messages[ Object.keys(this.input.$error)[0] ] && this.input.$invalid;
// Make sure all needed messages exists in this.messages otherwise create fallback
let self = this;
this.$scope.$watchCollection('nReplaceWithValidation.input.$validators', (newValue) => {
if(!newValue) return;

for(var validator in newValue) {

if( !self.messages.hasOwnProperty(validator) ) {
self.messages[validator] = self.messages.fallback;
}

}
});
}
}

Expand Down
4 changes: 1 addition & 3 deletions src/nReplaceWithValidation.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,9 @@
ng-show="!nReplaceWithValidation.input.$pristine || nReplaceWithValidation.input.$$parentForm.$submitted">

<span class="is-invalid-label" ng-message-exp="key" ng-repeat="(key, message) in nReplaceWithValidation.messages track by $index">
{{message}}
<span ng-bind-html="message"></span>
</span>


</span>
<span ng-if="nReplaceWithValidation.useFallback()" class="is-invalid-label" ng-bind-html="nReplaceWithValidation.messages.fallback"></span>

</div>

0 comments on commit 19a59db

Please sign in to comment.