-
-
Save merolhack/7423c29a141e9c43adaab03bcf2c8681 to your computer and use it in GitHub Desktop.
A simple angularjs with angular-ui modal form which includes validation on the client side.Thanks http://scotch.io/tutorials/javascript/angularjs-form-validation
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
var app = angular.module("modalFormApp", ['ui.bootstrap']); | |
app.controller("modalAccountFormController", ['$scope', '$uibModal', '$log', | |
function ($scope, $uibModal, $log) { | |
$scope.showForm = function () { | |
$scope.message = "Show Form Button Clicked"; | |
console.log($scope.message); | |
var uibModalInstance = $uibModal.open({ | |
templateUrl: 'modal-form.html', | |
controller: ModalInstanceCtrl, | |
scope: $scope, | |
resolve: { | |
userForm: function () { | |
return $scope.userForm; | |
} | |
} | |
}); | |
uibModalInstance.result.then(function (selectedItem) { | |
$scope.selected = selectedItem; | |
}, function () { | |
$log.info('Modal dismissed at: ' + new Date()); | |
}); | |
}; | |
}]); | |
var ModalInstanceCtrl = function ($scope, $uibModalInstance, userForm) { | |
$scope.form = {} | |
$scope.submitForm = function () { | |
if ($scope.form.userForm.$valid) { | |
console.log('user form is in scope'); | |
$uibModalInstance.close('closed'); | |
} else { | |
console.log('userform is not in scope'); | |
} | |
}; | |
$scope.cancel = function () { | |
$uibModalInstance.dismiss('cancel'); | |
}; | |
}; |
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 lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<title>Angular Modal Forms</title> | |
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous"> | |
<style> | |
body { padding-top:30px; } | |
</style> | |
</head> | |
<body ng-app="modalFormApp"> | |
<div class="container"> | |
<div class="col-sm-8 col-sm-offset-2"> | |
<!-- PAGE HEADER --> | |
<div class="page-header"> | |
<h1>AngularJS Form Validation</h1> | |
</div> | |
<div ng-controller="modalAccountFormController"> | |
<div class="page-body"> | |
<button class="btn btn-primary" ng-click="showForm()">Create Account</button> | |
</div> | |
</div> | |
</div> | |
</div> | |
<!-- JS ===================== --> | |
<!-- load angular --> | |
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.7/angular.min.js"></script> | |
<script src="https://code.angularjs.org/1.5.7/angular-animate.min.js"></script> | |
<script src="http://angular-ui.github.io/bootstrap/ui-bootstrap-tpls-1.3.3.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
<div class="modal-header"> | |
<h3>Create A New Account!</h3> | |
</div> | |
<form name="form.userForm" ng-submit="submitForm()" novalidate> | |
<div class="modal-body"> | |
<!-- NAME --> | |
<div class="form-group"> | |
<label>Name</label> | |
<input type="text" name="name" class="form-control" ng-model="name" required> | |
<p ng-show="form.userForm.name.$invalid && !form.userForm.name.$pristine" class="help-block">You name is required.</p> | |
</div> | |
<!-- USERNAME --> | |
<div class="form-group"> | |
<label>Username</label> | |
<input type="text" name="username" class="form-control" ng-model="user.username" ng-minlength="3" ng-maxlength="8" required> | |
<p ng-show="form.userForm.username.$error.minlength" class="help-block">Username is too short.</p> | |
<p ng-show="form.userForm.username.$error.maxlength" class="help-block">Username is too long.</p> | |
</div> | |
<!-- EMAIL --> | |
<div class="form-group"> | |
<label>Email</label> | |
<input type="email" name="email" class="form-control" ng-model="email" required> | |
<p ng-show="form.userForm.email.$invalid && !form.userForm.email.$pristine" class="help-block">Enter a valid email.</p> | |
</div> | |
</div> | |
<div class="modal-footer"> | |
<button type="submit" class="btn btn-primary" ng-disabled="form.userForm.$invalid">OK</button> | |
<button class="btn btn-warning" ng-click="cancel()">Cancel</button> | |
</div> | |
</form> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment