Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Formulário base para o post sobre validação
<!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>
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;
};
}
}
});
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