Last active
January 20, 2016 12:02
-
-
Save gabrielfeitosa/82f9ebc1b655e2b033d9 to your computer and use it in GitHub Desktop.
Formulário base para o post sobre validação
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
<!DOCTYPE html> | |
<html ng-app="app"> | |
<head> | |
<meta charset="utf-8" /> | |
<title>Blog do Gabriel Feitosa</title> | |
</head> | |
<body> | |
<h1>AngularJS: Validação de Formulário</h1> | |
<div ng-controller="CaixaController as vm"> | |
<form name="form" novalidate ng-submit="vm.sacar()"> | |
<table> | |
<tr> | |
<td>Quanto vai sacar?</td> | |
<td> <input type="number" name="valor" ng-model="vm.valor" required> </td> | |
</tr> | |
<tr> | |
<td> Senha: </td> | |
<td> <input type="password" name="senha" ng-model="vm.senha" required> </td> | |
</tr> | |
</table> | |
<br> | |
<button type="submit">Sacar</button> | |
</form> | |
</div> | |
<footer> | |
<hr/><a href="http://www.gabrielfeitosa.com"> Blog do Gabriel Feitosa</a> | |
</footer> | |
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script> | |
<script src="app.js"></script> | |
</body> | |
</html> |
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
angular.module('app').directive('saque', function() { | |
return { | |
require: 'ngModel', | |
controller: function($element) { | |
var ctrl = $element.controller('ngModel'); | |
ctrl.$validators.valor = | |
function(modelValue, viewValue) { | |
return viewValue && viewValue < 500; | |
}; | |
} | |
} | |
}); |
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
angular.module('app').directive('saqueAsync', function($timeout, $q) { | |
return { | |
require: 'ngModel', | |
controller: function($element) { | |
var ctrl = $element.controller('ngModel'); | |
ctrl.$asyncValidators.saqueAsync = | |
function(modelValue, viewValue) { | |
return $timeout(function() {}, 1800).then(function() { | |
var saldoDisponivel = 900; | |
if (!viewValue || viewValue > saldoDisponivel) { | |
return $q.reject(); | |
} | |
return $q.resolve(); | |
}).catch(function(rejection) { | |
ctrl.$setTouched(); | |
return $q.reject(rejection); | |
});; | |
}; | |
} | |
}; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment