Skip to content

Commit a58db97

Browse files
committed
Implement isolateForm directive for isolating form validation like option password changes
angular/angular.js#5858
1 parent 6a7117e commit a58db97

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

client/app/js/directives.js

+35
Original file line numberDiff line numberDiff line change
@@ -266,4 +266,39 @@ angular.module('GLDirectives', ['GLBrowserCrypto']).
266266
pgpPubkeyValidator: '@',
267267
}
268268
};
269+
}]).
270+
// isolateForm directive makes possible to isolate validation of nested forms
271+
directive('isolateForm', [function () {
272+
return {
273+
restrict: 'A',
274+
require: '?form',
275+
link: function (scope, elm, attrs, ctrl) {
276+
if (!ctrl) {
277+
return;
278+
}
279+
280+
// Do a copy of the controller
281+
var ctrlCopy = {};
282+
angular.copy(ctrl, ctrlCopy);
283+
284+
// Get the parent of the form
285+
var parent = elm.parent().controller('form');
286+
// Remove parent link to the controller
287+
parent.$removeControl(ctrl);
288+
289+
// Replace form controller with a "isolated form"
290+
var isolatedFormCtrl = {
291+
$setValidity: function (validationToken, isValid, control) {
292+
ctrlCopy.$setValidity(validationToken, isValid, control);
293+
parent.$setValidity(validationToken, true, ctrl);
294+
},
295+
$setDirty: function () {
296+
elm.removeClass('ng-pristine').addClass('ng-dirty');
297+
ctrl.$dirty = true;
298+
ctrl.$pristine = false;
299+
},
300+
};
301+
angular.extend(ctrl, isolatedFormCtrl);
302+
}
303+
};
269304
}]);

client/app/views/admin/users.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@
113113
<div class="text-danger" data-ng-show="editUser.email.$error.required" data-translate>This field is mandatory</div>
114114
<div class="text-danger" data-ng-show="editUser.email.$error.pattern" data-translate>Invalid email address</div>
115115
</div>
116-
<div class="form-group">
116+
<div class="form-group" data-ng-form="userPasswordForm" data-isolate-form data-ng-class="{'has-error': passwordStrength.score > 0 && !userPassword.password.$valid}">
117117
<label data-translate>Password</label>
118118
<input name="password" class="form-control" data-ng-model="user.password" maxlength="{{node.maximum_namesize}}" type="password" zxcvbn="passwordStrength" zx-min-score="3" />
119119
<zx-password-meter value="passwordStrength"></zx-password-meter>

0 commit comments

Comments
 (0)