Skip to content

Instantly share code, notes, and snippets.

@jwcarroll
Last active December 17, 2015 19:09
Show Gist options
  • Save jwcarroll/e0018b9d20ddf5c12e9f to your computer and use it in GitHub Desktop.
Save jwcarroll/e0018b9d20ddf5c12e9f to your computer and use it in GitHub Desktop.
(function(){
'use strict';
function createWatchExpression(formName) {
var props = ['$pristine', '$dirty', '$valid', '$pending', '$submitted'];
return _.map(props, function (prop) {
return formName + "." + prop;
});
}
function BootstrapValidationDirective() {
return {
restrict: 'A',
require: 'form',
link: function (scope, elem, attrs, formCtrl) {
scope.$watchGroup(createWatchExpression(formCtrl.$name), function () {
var inputs = elem.find('input');
angular.forEach(inputs, function (input) {
var control = formCtrl[$(input).attr('name')];
var parent = $(input).parent();
if (control) {
parent.toggleClass('has-error', control.$invalid);
}
});
});
}
};
}
angular.module('my-app')
.directive('bsValidation', BootstrapValidationDirective);
}());
<form name="ctrl.userForm" bs-validation>
<div class="form-group">
<label class="control-label">First Name</label>
<input type="text" name="Firstname" class="form-control" placeholder="First Name" ng-model="ctrl.user.Firstname">
</div>
<div class="form-group">
<label class="control-label">Last Name</label>
<input type="text" name="Lastname" class="form-control" placeholder="Last Name" ng-model="ctrl.user.Lastname">
</div>
</form>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment