Last active
December 17, 2015 19:09
-
-
Save jwcarroll/e0018b9d20ddf5c12e9f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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); | |
}()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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