Skip to content

Commit e6bc251

Browse files
Create oscUnique directive to provide unique-in-list validation on DOM nodes with ng-model attribute
1 parent 13fa7cd commit e6bc251

File tree

4 files changed

+66
-3
lines changed

4 files changed

+66
-3
lines changed

app/index.html

+1
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,7 @@
179179
<script src="scripts/directives/oscImageSummary.js"></script>
180180
<script src="scripts/directives/oscKeyValues.js"></script>
181181
<script src="scripts/directives/oscRouting.js"></script>
182+
<script src="scripts/directives/oscUnique.js"></script>
182183
<script src="scripts/directives/replicas.js"></script>
183184
<script src="scripts/directives/resources.js"></script>
184185
<script src="scripts/directives/overviewDeployment.js"></script>

app/scripts/directives/oscKeyValues.js

-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ angular.module("openshiftConsole")
4545
$scope.value = null;
4646
$scope.form.$setPristine();
4747
$scope.form.$setUntouched();
48-
$scope.form.$setValidity();
4948
}
5049
};
5150
$scope.deleteEntry = function(key) {

app/scripts/directives/oscUnique.js

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
'use strict';
2+
// oscUnique is a validation directive
3+
// use:
4+
// Put it on an input or other DOM node with an ng-model attribute.
5+
// Pass a list (array, or object) via osc-unique="list"
6+
//
7+
// Sets model $valid true||false
8+
// - model is valid so long as the item is not already in the list
9+
//
10+
// Key off $valid to enable/disable/sow/etc other objects
11+
//
12+
// Validates that the ng-model is unique in a list of values.
13+
// ng-model: 'foo'
14+
// oscUnique: ['foo', 'bar', 'baz'] // false, the string 'foo' is in the list
15+
// oscUnique: [1,2,4] // true, the string 'foo' is not in the list
16+
// oscUnique: {foo: true, bar: false} // false, the object has key 'foo'
17+
// NOTES:
18+
// - non-array values passed to oscUnqiue will be transformed into an array.
19+
// - oscUnqiue: 'foo' => [0,1,2] (probably not what you want, so don't pass a string)
20+
// - objects passed will be converted to a list of object keys.
21+
// - { foo: false } would still be invalid, because the key exists (value is ignored)
22+
// - recommended to pass an array
23+
//
24+
// Example:
25+
// - prevent a button from being clickable if the input value has already been used
26+
// <input ng-model="key" osc-unique="keys" />
27+
// <button ng-disabled="form.key.$error.oscUnique" ng-click="submit()">Submit</button>
28+
//
29+
angular.module('openshiftConsole')
30+
.directive('oscUnique', function() {
31+
return {
32+
restrict: 'A',
33+
scope: {
34+
oscUnique: '='
35+
},
36+
require: 'ngModel',
37+
link: function($scope, $elem, $attrs, ctrl) {
38+
var list = [];
39+
40+
$scope.$watchCollection('oscUnique', function(newVal) {
41+
list = _.isArray(newVal) ?
42+
newVal :
43+
_.keys(newVal);
44+
});
45+
46+
ctrl.$parsers.unshift(function(value) {
47+
// is valid so long as it doesn't already exist
48+
ctrl.$setValidity('oscUnique', !_.includes(list, value));
49+
return value;
50+
});
51+
}
52+
};
53+
});

app/views/directives/osc-key-values.html

+12-2
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@
77
name="key"
88
ng-attr-placeholder="{{keyTitle}}"
99
ng-model="key"
10+
ng-model-options="{ debounce: 200 }"
1011
autocorrect="off"
1112
autocapitalize="off"
1213
spellcheck="false"
13-
osc-input-validator="key">
14+
osc-input-validator="key"
15+
osc-unique="entries">
1416
</div>
1517
<div class="form-group" ng-class="{'has-error': form.value.$error.oscValueValid}">
1618
<input class="form-control"
@@ -23,7 +25,15 @@
2325
spellcheck="false"
2426
osc-input-validator="value">
2527
</div>
26-
<button class="btn btn-default" ng-click="addEntry()" ng-disabled="form.key.$error.oscKeyValid || form.value.$error.oscValueValid">Add</button>
28+
<button
29+
class="btn btn-default"
30+
ng-click="addEntry()"
31+
ng-disabled="form.$invalid || !key || !value">
32+
Add
33+
</button>
34+
<div class="has-error" ng-show="form.key.$error.oscUnique">
35+
<span class="help-block">Duplicate {{(keyTitle || 'key') | lowercase}}: {{key}}</span>
36+
</div>
2737
<div class="has-error" ng-show="form.key.$error.oscKeyValid">
2838
<span class="help-block">Please enter a valid {{setErrorText(keyValidator)}}
2939
<span class="help action-inline" ng-if="keyValidationTooltip">

0 commit comments

Comments
 (0)