Created
December 19, 2017 15:28
-
-
Save tatums/4d0be753347931cb0b021b5ab7a76130 to your computer and use it in GitHub Desktop.
conditional required form fields in 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
| <!DOCTYPE html> | |
| <html> | |
| <head> | |
| <meta charset="UTF-8"> | |
| <meta name="viewport" content="width=device-width, initial-scale=1"> | |
| <title>Hello World!</title> | |
| <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script> | |
| <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.9/angular.min.js"></script> | |
| <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/foundation/5.4.7/css/foundation.min.css" /> | |
| <script src="https://cdnjs.cloudflare.com/ajax/libs/foundation/5.4.7/js/foundation.min.js"></script> | |
| </head> | |
| <body ng-app="myApp"> | |
| <div ng-controller="myCtrl"> | |
| <h1>{{ pageTitle }}</h1> | |
| <form name="myForm" ng-submit="submit(form, myForm)" novalidate> | |
| <div class="row"> | |
| <input type="text" name="title" id="title" ng-model="form.title" placeholder="Epic Article" ng-required="saveType=='draft' || saveType == 'published'"> | |
| <div ng-if="myForm.title.$invalid">There is an error with the field...</div> | |
| </div> | |
| <div class="row"> | |
| <input type="text" name="short_description" id="short_description" ng-model="form.short_description" placeholder="..." ng-required="saveType == 'published'"> | |
| <div ng-if="myForm.short_description.$invalid">There is an error with the field...</div> | |
| </div> | |
| <button ng-click="saveType = 'published'"type="submit">Publish</button> | |
| <button ng-click="saveType = 'draft'"type="submit">Save (draft)</button> | |
| </form> | |
| <!-- | |
| <code style="margin-top: 100px;"> | |
| {{ myForm }} | |
| </code> | |
| --> | |
| </div> | |
| <script type="text/javascript" > | |
| angular.module('myApp', []); | |
| angular.module('myApp') | |
| .controller('myCtrl', function ($scope) { | |
| $scope.pageTitle = "Angular form foo" | |
| $scope.form = {} | |
| $scope.submit = function(form, myForm) { | |
| if ( myForm.$valid ) { | |
| console.log("YUP, I'm valid") | |
| } else { | |
| console.log("NOPE, invalid") | |
| } | |
| } | |
| }) | |
| </script> | |
| </body> | |
| </html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment