Last active
April 23, 2019 17:59
-
-
Save kudchikarsk/37d6b5f82b66940de6ebffd66b22734a to your computer and use it in GitHub Desktop.
Form input directive for Angularjs
This file contains hidden or 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 (module) { | |
var watcherFor = function (form, name) { | |
return function () { | |
if (name && form[name]) { | |
return form[name].$invalid; | |
} | |
}; | |
}; | |
var updaterFor = function (element) { | |
return function (hasError) { | |
if (hasError) { | |
element.removeClass("has-success") | |
.addClass("has-error"); | |
} else { | |
element.addClass("has-success") | |
.removeClass("has-error"); | |
} | |
}; | |
}; | |
var setupDom = function (element) { | |
var input = element[0].querySelector("input, textarea, select, otf-rating"); | |
var type = input.getAttribute("type"); | |
var name = input.getAttribute("name"); | |
if (type !== "checkbox" && type !== "radio") { | |
input.classList.add("form-control"); | |
} | |
var label = element[0].querySelector("label"); | |
label.classList.add("control-label"); | |
return name; | |
}; | |
var addMessages = function(form, element, name, $compile, scope) { | |
var messages = "<div class='help-block' ng-messages='" + | |
form.$name + "." + name + ".$error" + | |
"' ng-messages-include='templates/messages.html'><div>"; | |
element.append($compile(messages)(scope)); | |
}; | |
var link = function ($compile) { | |
return function(scope, element, attributes, form) { | |
var name = setupDom(element); | |
addMessages(form, element, name, $compile, scope); | |
scope.$watch(watcherFor(form, name), updaterFor(element)); | |
} | |
}; | |
var forminput = function ($compile) { | |
return { | |
restrict: "A", | |
require: "^form", | |
link: link($compile) | |
}; | |
}; | |
module.directive("forminput", forminput); | |
}(angular.module("forms"))); | |
/// | |
/* messages.html template | |
<span ng-message="required">This field is required</span> | |
<span ng-message="min">Too small</span> | |
<span ng-message="max">Too large</span> | |
<span ng-message="email">Enter a valid email</span> | |
<span ng-message="startsWith">Incorrect start</span> | |
<span ng-message="username">Username is unavailble</span> | |
*/ | |
/* index.html form example | |
<form ng-submit="model.submit(profileForm.$valid)" name="profileForm" novalidate> | |
<h2>Sign Up Now!</h2> | |
<div forminput> | |
<label for="username">Your user name:</label> | |
<input type="text" id="username" name="username" required placeholder="User name" | |
ng-model="model.user.username" username starts-with="S"> | |
</div> | |
<div forminput> | |
<label for="email">Your email:</label> | |
<input type="email" id="email" name="email" required placeholder="Email address" | |
ng-model="model.user.email"> | |
</div> | |
<div forminput> | |
<label for="website">Your website:</label> | |
<input type="url" id="website" name="website" placeholder="Website" | |
ng-model="model.user.website"> | |
</div> | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment