Skip to content

Commit b1248aa

Browse files
committed
Bugfix for angular 1.2
$commitViewValue is 1.3 only... Also updated bootstrap-example.html so it has a ng-submit that validates the entire form.
1 parent 413f9f6 commit b1248aa

File tree

3 files changed

+22
-5
lines changed

3 files changed

+22
-5
lines changed

examples/bootstrap-example.html

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ <h1>Schema Form Example</h1>
3535
<div class="row">
3636
<div class="col-sm-4">
3737
<h3>The Generated Form</h3>
38-
<form name="ngform" sf-model="modelData" sf-form="form" sf-schema="schema"></form>
38+
<form name="ngform" sf-model="modelData" sf-form="form" sf-schema="schema" ng-submit="submitForm(ngform,modelData)"></form>
3939
<h3>Model</h3>
4040
<pre ng-cloak>{{pretty()}}</pre>
4141
</div>
@@ -60,10 +60,8 @@ <h3>Schema</h3>
6060
<script type="text/javascript" src="http://cdn.jsdelivr.net/g/[email protected]"></script>
6161
<script type="text/javascript" src="../bower_components/tv4/tv4.js"></script>
6262
<script type="text/javascript" src="../bower_components/ace-builds/src-min-noconflict/ace.js"></script>
63-
6463
<script type="text/javascript" src="../bower_components/angular/angular.min.js"></script>
6564
<script type="text/javascript" src="../bower_components/angular-sanitize/angular-sanitize.min.js"></script>
66-
6765
<!-- <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.21/angular.min.js"></script> -->
6866
<!-- <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.21/angular-sanitize.min.js"></script> -->
6967

@@ -74,7 +72,7 @@ <h3>Schema</h3>
7472
<script type="text/javascript" src="../bower_components/pickadate/lib/picker.date.js"></script>
7573
<script type="text/javascript" src="../bower_components/pickadate/lib/translations/sv_SE.js"></script>
7674

77-
<script type="text/javascript" src="../dist/schema-form.min.js"></script>
75+
<script type="text/javascript" src="../dist/schema-form.js"></script>
7876
<script type="text/javascript" src="../dist/bootstrap-decorator.min.js"></script>
7977
<script type="text/javascript" src="../dist/bootstrap-datepicker.min.js"></script>
8078
<script type="text/javascript">
@@ -148,6 +146,15 @@ <h3>Schema</h3>
148146
alert(msg);
149147
};
150148

149+
$scope.submitForm = function(form, model) {
150+
// First we broadcast an event so all fields validate themselves
151+
$scope.$broadcast('schemaFormValidate');
152+
// Then we check if the form is valid
153+
if (form.$valid) {
154+
alert('You did it!');
155+
}
156+
}
157+
151158
});
152159

153160
</script>

src/directives/schema-validate.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,12 @@ angular.module('schemaForm').directive('schemaValidate',['sfValidator',function(
5353

5454
// Listen to an event so we can validate the input on request
5555
scope.$on('schemaFormValidate',function() {
56-
ngModel.$commitViewValue(true);
56+
57+
if (ngModel.$commitViewValue) {
58+
ngModel.$commitViewValue(true);
59+
} else {
60+
ngModel.$setViewValue(ngModel.$viewValue);
61+
}
5762
});
5863

5964
//This works since we now we're inside a decorator and that this is the decorators scope.

src/services/validator.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ angular.module('schemaForm').factory('sfValidator',[function(){
1717

1818
var schema = form.schema;
1919

20+
if (!schema) {
21+
//Nothings to Validate
22+
return value;
23+
}
24+
2025
//Type cast and validate against schema.
2126
//Basic types of json schema sans array and object
2227
if (schema.type === 'integer') {

0 commit comments

Comments
 (0)