Skip to content

Commit 3edd77d

Browse files
committed
Merge branch 'feature/x-should-be-in-a-form' into development
2 parents 8e8cb1a + ee3c506 commit 3edd77d

File tree

3 files changed

+57
-1
lines changed

3 files changed

+57
-1
lines changed

docs/index.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ Documentation
44
1. [Basic Usage](#basic-usage)
55
1. [Handling Submit](#handling-submit)
66
1. [Global Options](#global-options)
7+
1. [Form defaults in schema](#form-defaults-in-schema)
78
1. [Form types](#form-types)
89
1. [Default form types](#default-form-types)
910
1. [Form definitions](#form-definitions)
@@ -174,7 +175,29 @@ Ex.
174175
</div>
175176
```
176177
178+
Form defaults in schema
179+
-----------------------
180+
Its recommended to split presentation and validation into a form definition and a json schema. But
181+
if you for some reason can't do this, but *do* have the power to change the schema, you can supply form
182+
default values within the schema using the custom attribute `x-schema-form`. `x-schema-form` should
183+
be a form object and acts as form definition defaults for that field.
177184
185+
Example schema.
186+
```js
187+
{
188+
"type": "object",
189+
"properties": {
190+
"comment": {
191+
"type": "string",
192+
"title": "Comment",
193+
"x-schema-form": {
194+
"type": "textarea",
195+
"placeholder": "Don't hold back"
196+
}
197+
}
198+
}
199+
}
200+
```
178201
179202
Form types
180203
----------

src/services/schema-form.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,15 @@ angular.module('schemaForm').provider('schemaForm',
4040
var def;
4141
for (var i = 0; i < rules.length; i++) {
4242
def = rules[i](name, schema, options);
43+
4344
//first handler in list that actually returns something is our handler!
4445
if (def) {
46+
47+
// Do we have form defaults in the schema under the x-schema-form-attribute?
48+
if (def.schema['x-schema-form'] && angular.isObject(def.schema['x-schema-form'])) {
49+
def = angular.extend(def, def.schema['x-schema-form']);
50+
}
51+
4552
return def;
4653
}
4754
}
@@ -67,14 +74,16 @@ angular.module('schemaForm').provider('schemaForm',
6774
if (schema.minimum) { f.minimum = schema.minimum + (schema.exclusiveMinimum ? 1 : 0); }
6875
if (schema.maximum) { f.maximum = schema.maximum - (schema.exclusiveMaximum ? 1 : 0); }
6976

70-
//Non standard attributes
77+
// Non standard attributes (DONT USE DEPRECATED)
78+
// If you must set stuff like this in the schema use the x-schema-form attribute
7179
if (schema.validationMessage) { f.validationMessage = schema.validationMessage; }
7280
if (schema.enumNames) { f.titleMap = canonicalTitleMap(schema.enumNames, schema['enum']); }
7381
f.schema = schema;
7482

7583
// Ng model options doesn't play nice with undefined, might be defined
7684
// globally though
7785
f.ngModelOptions = f.ngModelOptions || {};
86+
7887
return f;
7988
};
8089

test/services/schema-form-test.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,30 @@ describe('schemaForm', function() {
257257
});
258258
});
259259

260+
it.only('should handle x-schema-form defaults',function(){
261+
inject(function(schemaForm){
262+
263+
var schema = {
264+
"type": "object",
265+
"properties": {
266+
"name": {
267+
"title": "Name",
268+
"description": "Gimme yea name lad",
269+
"type": "string",
270+
"x-schema-form": {
271+
"type": "textarea"
272+
}
273+
}
274+
}
275+
};
276+
277+
278+
279+
var f = schemaForm.defaults(schema,{});
280+
f.form[0].type.should.be.eq('textarea');
281+
});
282+
});
283+
260284
it('should ignore parts of schema in ignore list',function(){
261285
inject(function(schemaForm){
262286

0 commit comments

Comments
 (0)