Skip to content

Instantly share code, notes, and snippets.

@hyrmn
Created October 5, 2013 20:16
Show Gist options
  • Select an option

  • Save hyrmn/6845551 to your computer and use it in GitHub Desktop.

Select an option

Save hyrmn/6845551 to your computer and use it in GitHub Desktop.
Wire-up validation
angular.module('ui.textbox', [])
.directive('textbox', [function () {
return {
restrict: 'E',
replace: true,
require: '^form',
scope: {
model: '=',
name: '=',
uiName: '@',
blur: '&'
},
template:
'<div class="control-group" ng-class="{error: formElement.$dirty && formElement.$invalid}">' +
' <label class="control-label" for="{{name}}" title="{{uiName}}">{{uiName}}</label>' +
' <div class="controls">' +
' <input type="text" ng-model="model" placeholder="{{uiName}}" class="input-medium" ng-blur="blur()">' +
' <span ng-show="formElement.$dirty && formElement.$error.required" class="help-inline">{{uiName}} is required.</span>' +
' </div>' +
'</div>',
compile: function (tElem, tAttrs) {
var $input = $(tElem).find('input').attr({
id: tAttrs.name,
name: tAttrs.name,
});
if (tAttrs.required === "true") {
$input.attr({
required: true
});
}
if (tAttrs.size) {
$input.removeClass('input-medium');
$input.addClass(tAttrs.size);
}
return function(scope, element, attrs, form) {
scope.formElement = form[attrs.name];
};
},
};
}]);
<textbox model="questions.QuestionOne.Question" name="QuestionOne" ui-name="Question One" required="true" size="input-large" />
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment