|
| 1 | +<!DOCTYPE html> |
| 2 | +<html> |
| 3 | + <head> |
| 4 | + <title>Custom validators, async validators etc</title> |
| 5 | + <link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css"> |
| 6 | + <link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap-theme.min.css"> |
| 7 | + </head> |
| 8 | + <body ng-app="test" class="container" ng-controller="TestCtrl"> |
| 9 | + |
| 10 | + <h3>Demo of custom validators, async validators and parsers</h3> |
| 11 | + <span>Check the source</span> |
| 12 | + <form name="theForm"> |
| 13 | + <div sf-schema="schema" sf-form="form" sf-model="model"></div> |
| 14 | + <div> |
| 15 | + The form is <em ng-show="theForm.$pristine">pristine</em><em ng-show="theForm.$dirty">dirty</em> |
| 16 | + and <em ng-show="theForm.$valid">valid</em><em ng-show="!theForm.$valid">invalid</em>. |
| 17 | + </div> |
| 18 | + <div>{{prettyModel}}</div> |
| 19 | + </form> |
| 20 | + |
| 21 | + <script type="text/javascript" src="../bower_components/tv4/tv4.js"></script> |
| 22 | + <script type="text/javascript" src="../bower_components/angular/angular.min.js"></script> |
| 23 | + <script type="text/javascript" src="../bower_components/angular-sanitize/angular-sanitize.min.js"></script> |
| 24 | + |
| 25 | + <script type="text/javascript" src="../bower_components/objectpath/lib/ObjectPath.js"></script> |
| 26 | + |
| 27 | + <script type="text/javascript" src="../dist/schema-form.js"></script> |
| 28 | + <script type="text/javascript" src="../dist/bootstrap-decorator.min.js"></script> |
| 29 | + |
| 30 | + <script> |
| 31 | + angular.module('test', ['schemaForm']).controller('TestCtrl', function($scope, $q, $timeout) { |
| 32 | + |
| 33 | + $scope.schema = { |
| 34 | + "type": "object", |
| 35 | + "title": "Comment", |
| 36 | + "properties": { |
| 37 | + "name": { |
| 38 | + "title": "Name", |
| 39 | + "type": "string" |
| 40 | + }, |
| 41 | + "email": { |
| 42 | + "title": "Email", |
| 43 | + "type": "string", |
| 44 | + "pattern": "^\\S+@\\S+$", |
| 45 | + "description": "Email will be used for evil." |
| 46 | + }, |
| 47 | + "comment": { |
| 48 | + "title": "Comment", |
| 49 | + "type": "string" |
| 50 | + } |
| 51 | + }, |
| 52 | + "required": ["name","email","comment"] |
| 53 | + }; |
| 54 | + |
| 55 | + $scope.form = [ |
| 56 | + { |
| 57 | + key: 'name', |
| 58 | + placeholder: 'Anything but "Bob"', |
| 59 | + $asyncValidators: { |
| 60 | + 'async': function(name) { |
| 61 | + var deferred = $q.defer(); |
| 62 | + $timeout(function(){ |
| 63 | + if (angular.isString(name) && name.toLowerCase().indexOf('bob') !== -1) { |
| 64 | + deferred.reject(); |
| 65 | + } else { |
| 66 | + deferred.resolve(); |
| 67 | + } |
| 68 | + }, 500); |
| 69 | + return deferred.promise; |
| 70 | + } |
| 71 | + }, |
| 72 | + validationMessage: { |
| 73 | + 'async': "Wooohoo thats not an OK name!" |
| 74 | + } |
| 75 | + |
| 76 | + }, |
| 77 | + { |
| 78 | + key: 'email', |
| 79 | + placeholder: 'Not MY email', |
| 80 | + ngModel: function(ngModel) { |
| 81 | + ngModel.$validators.myMail = function(value) { |
| 82 | + return value !== 'david.lgj@gmail.com'; |
| 83 | + }; |
| 84 | + }, |
| 85 | + validationMessage: { |
| 86 | + 'myMail': "Thats my mail!" |
| 87 | + } |
| 88 | + }, |
| 89 | + { |
| 90 | + "key": "comment", |
| 91 | + "type": "textarea", |
| 92 | + "placeholder": "Make a comment, write 'damn' and check the model", |
| 93 | + $parsers: [ |
| 94 | + function(value) { |
| 95 | + if (value && value.replace) { |
| 96 | + return value.replace(/(damn|fuck|apple)/,'#!@%&'); |
| 97 | + } |
| 98 | + return value; |
| 99 | + } |
| 100 | + ] |
| 101 | + }, |
| 102 | + { |
| 103 | + "type": "submit", |
| 104 | + "style": "btn-info", |
| 105 | + "title": "OK" |
| 106 | + } |
| 107 | + ]; |
| 108 | + |
| 109 | + $scope.model = {}; |
| 110 | + |
| 111 | + $scope.$watch('model', function(value){ |
| 112 | + if (value) { |
| 113 | + $scope.prettyModel = JSON.stringify(value, undefined, 2); |
| 114 | + } |
| 115 | + }, true); |
| 116 | + |
| 117 | + }); |
| 118 | + |
| 119 | + </script> |
| 120 | + |
| 121 | + |
| 122 | + </body> |
| 123 | +</html> |
0 commit comments