Skip to content

Instantly share code, notes, and snippets.

@malixsys
Last active July 11, 2016 07:42
Show Gist options
  • Star 17 You must be signed in to star a gist
  • Fork 7 You must be signed in to fork a gist
  • Save malixsys/8721568 to your computer and use it in GitHub Desktop.
Save malixsys/8721568 to your computer and use it in GitHub Desktop.
ionic framework validation
form i.icon.error {
color: $assertive;
}
form input + i.icon.error {
display: none;
margin-left: 8px;
}
form.ng-submitted input.ng-invalid + i.icon.error {
display: block;
}
form .has-error {
border-left: 5px solid darken($assertive, 15%);
}
form .has-success {
border-left: 5px solid darken($energized, 15%);
}
form.ng-submitted input.ng-valid + i.icon.error {
display: none;
}
angular.module('app', [...] )
.directive('onValidSubmit', ['$parse', '$timeout', function($parse, $timeout) {
return {
require: '^form',
restrict: 'A',
link: function(scope, element, attrs, form) {
form.$submitted = false;
var fn = $parse(attrs.onValidSubmit);
element.on('submit', function(event) {
scope.$apply(function() {
element.addClass('ng-submitted');
form.$submitted = true;
if (form.$valid) {
if (typeof fn === 'function') {
fn(scope, {$event: event});
}
}
});
});
}
}
}])
.directive('validated', ['$parse', function($parse) {
return {
restrict: 'AEC',
require: '^form',
link: function(scope, element, attrs, form) {
var inputs = element.find("*");
for(var i = 0; i < inputs.length; i++) {
(function(input){
var attributes = input.attributes;
if (attributes.getNamedItem('ng-model') != void 0 && attributes.getNamedItem('name') != void 0) {
var field = form[attributes.name.value];
if (field != void 0) {
scope.$watch(function() {
return form.$submitted + "_" + field.$valid;
}, function() {
if (form.$submitted != true) return;
var inp = angular.element(input);
if (inp.hasClass('ng-invalid')) {
element.removeClass('has-success');
element.addClass('has-error');
} else {
element.removeClass('has-error').addClass('has-success');
}
});
}
}
})(inputs[i]);
}
}
}
}])
;
<form novalidate="novalidate" on-valid-submit="doLogin()">
<div class="list card">
<div class="item item-divider">Enter your info:</div>
<label class="item item-input validated">
<span class="input-label">Email</span>
<input type="text" ng-model="user.email" required="required" name="email">
<i class="icon ion-alert-circled error"></i>
</label>
<label class="item item-input validated">
<span class="input-label">Password</span>
<input type="password" ng-model="user.password" name="password" required="required">
<i class="icon ion-alert-circled error"></i>
</label>
</div>
<div class="padding">
<button type="submit" class="button icon-right ion-chevron-right button-block button-energized">
login
</button>
</div>
</form>
@malixsys
Copy link
Author

@calendee
Copy link

calendee commented Jul 3, 2014

@malixsys : See this fork. https://gist.github.com/calendee/829df21ea167a1c919da

I pulled the function out of the for loop to prevent creating a function inside a loop. Minor hit but also saves jshint errors.

FYI : Never done gists before; so, don't see a way to do a pull request. Might not be a way.

@squiso
Copy link

squiso commented Sep 23, 2014

Thanks for sharing! I needed form validation for a project and finding this solution really helped me.

@kowenzhang
Copy link

i put "pattern=".{3,} required" on an input . seems like it dosen't work on pattern validation. This input need a min length validation , is there any way to fix this?

@mick-cs
Copy link

mick-cs commented Mar 6, 2015

TypeError: Cannot read property 'value' of undefined

@RohitDhamal
Copy link

Thanks for sharing this. I already did validation according to my need, but they were not working properly. Your validation helped me full-fill what I wanted. Thank you. :-)

@akz92
Copy link

akz92 commented Nov 18, 2015

Thank you very much! This really helped me!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment