Skip to content

Commit 26facfe

Browse files
committed
Global option to disable setting of defaults
1 parent 9f34ebe commit 26facfe

File tree

3 files changed

+56
-11
lines changed

3 files changed

+56
-11
lines changed

docs/index.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,7 @@ attribute which should be placed along side `sf-schema`.
192192
| supressPropertyTitles | by default schema form uses the property name in the schema as a title if none is specified, set this to true to disable that behavior |
193193
| formDefaults | an object that will be used as a default for all form definitions |
194194
| validationMessage | an object or a function that will be used as default validation message for all fields. See [Validation Messages](#validation-messages) for details. |
195+
| setSchemaDefaults | boolean, set to false an no defaults from the schema will be set on the model. |
195196
196197
*formDefaults* is mostly useful for setting global [ngModelOptions](#ngmodeloptions)
197198
i.e. changing the entire form to validate on blur.
@@ -624,7 +625,7 @@ General options most field types can handle:
624625
onChange: "valueChanged(form.key,modelValue)", // onChange event handler, expression or function
625626
feedback: false, // Inline feedback icons
626627
disableSuccessState: false, // Set true to NOT apply 'has-success' class to a field that was validated successfully
627-
disableErrorState: false, // Set true to NOT apply 'has-error' class to a field that failed validation
628+
disableErrorState: false, // Set true to NOT apply 'has-error' class to a field that failed validation
628629
placeholder: "Input...", // placeholder on inputs and textarea
629630
ngModelOptions: { ... }, // Passed along to ng-model-options
630631
readonly: true, // Same effect as readOnly in schema. Put on a fieldset or array

src/directives/schema-form.js

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -119,14 +119,16 @@ angular.module('schemaForm')
119119
$compile(element.children())(childScope);
120120

121121
//ok, now that that is done let's set any defaults
122-
schemaForm.traverseSchema(schema, function(prop, path) {
123-
if (angular.isDefined(prop['default'])) {
124-
var val = sfSelect(path, scope.model);
125-
if (angular.isUndefined(val)) {
126-
sfSelect(path, scope.model, prop['default']);
122+
if (!scope.options || scope.options.setSchemaDefaults !== false) {
123+
schemaForm.traverseSchema(schema, function(prop, path) {
124+
if (angular.isDefined(prop['default'])) {
125+
var val = sfSelect(path, scope.model);
126+
if (angular.isUndefined(val)) {
127+
sfSelect(path, scope.model, prop['default']);
128+
}
127129
}
128-
}
129-
});
130+
});
131+
}
130132

131133
scope.$emit('sf-render-finished', element);
132134
};

test/directives/schema-form-test.js

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -494,6 +494,48 @@ describe('directive',function(){
494494
});
495495
});
496496

497+
it('should honor defaults in schema unless told not to',function(){
498+
499+
inject(function($compile,$rootScope){
500+
var scope = $rootScope.$new();
501+
scope.person = {
502+
name: 'Foobar'
503+
};
504+
505+
scope.schema = {
506+
"type": "object",
507+
"properties": {
508+
"name": {
509+
"type": "string",
510+
"default": "Bar"
511+
},
512+
"nick": {
513+
"type": "string",
514+
"default": "Zeb"
515+
},
516+
"alias": {
517+
"type": "string"
518+
},
519+
}
520+
};
521+
522+
scope.form = ["*"];
523+
524+
scope.options = {setSchemaDefaults: false};
525+
526+
var tmpl = angular.element('<form sf-options="options" sf-schema="schema" sf-form="form" sf-model="person"></form>');
527+
528+
$compile(tmpl)(scope);
529+
$rootScope.$apply();
530+
531+
scope.person.name.should.be.equal('Foobar');
532+
expect(scope.person.nick).to.be.undefined;
533+
expect(scope.person.alias).to.be.undefined;
534+
535+
});
536+
});
537+
538+
497539
it('should handle schema form default in deep structure',function(){
498540

499541
inject(function($compile,$rootScope){
@@ -1950,7 +1992,7 @@ describe('directive',function(){
19501992
}
19511993
};
19521994
scope.form = [field.form];
1953-
1995+
19541996
var tmpl = angular.element('<form name="theForm" sf-schema="schema" sf-form="form" sf-model="model"></form>');
19551997
$compile(tmpl)(scope);
19561998
$rootScope.$apply();
@@ -1978,7 +2020,7 @@ describe('directive',function(){
19782020
}
19792021
};
19802022
scope.form = [field.form];
1981-
2023+
19822024
var tmpl = angular.element('<form name="theForm" sf-schema="schema" sf-form="form" sf-model="model"></form>');
19832025
$compile(tmpl)(scope);
19842026
$rootScope.$apply();
@@ -1993,4 +2035,4 @@ describe('directive',function(){
19932035
});
19942036
});
19952037
});
1996-
});
2038+
});

0 commit comments

Comments
 (0)