Skip to content

[WIP] oneOf/anyOf support #505

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Auto select form based on model
Change model when form is changed
  • Loading branch information
gazpachoking committed Aug 14, 2015
commit 16a48b3009a07e7578a46377a962c06a0062adf6
2 changes: 1 addition & 1 deletion src/directives/decorators/bootstrap/formselect.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<div sf-form-select class="schema-form-form-select {{form.htmlClass}}">
<div sf-form-select="form" class="schema-form-form-select {{form.htmlClass}}">
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should probably create a simple one for core.

<legend ng-class="{'sr-only': !showTitle() }">{{ form.title }}</legend>
<div class="help-block" ng-show="form.description" ng-bind-html="form.description"></div>
<select ng-model="selectedForm"
Expand Down
35 changes: 35 additions & 0 deletions src/directives/formselect.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,42 @@ angular.module('schemaForm').directive('sfFormSelect', ['sfSelect', 'schemaForm'
require: '?ngModel',
link: function(scope, element, attrs, ngModel) {
scope.selectedForm = 0;
// Keeps the model data for each form
var formData = [];
// TODO: Watch the model value and pick a form to match it
scope.$parent.$watch(attrs.sfFormSelect, function(form) {
if (!form) {
return;
}

var model = sfSelect(form.key, scope.model);

// Watch the model value and change to a form that matches it
var key = sfPath.normalize(form.key);
scope.$parent.$watch('model' + key, function (value) {
model = scope.modelData = value;
// If the selected form still validates, make sure we don't change it
if (angular.isNumber(scope.selectedForm)) {
var r = sfValidator.validate(form.items[scope.selectedForm], model);
if (r.valid) return;
}
// Search for a form that is valid for the given model data
for (var i = 0; i < form.items.length; i++) {
var result = sfValidator.validate(form.items[i], model);
if (result.valid) {
scope.selectedForm = i;
break;
}
}
});

// Restore data associated with selected form
scope.$watch('selectedForm', function(selectedForm, oldForm) {
formData[oldForm] = model;
if (formData[selectedForm]) sfSelect(form.key, scope.model, formData[selectedForm]);
// TODO: Fill defaults if we don't have data for this form
});
});
}
};
}
Expand Down
1 change: 1 addition & 0 deletions src/services/schema-form.js
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,7 @@ angular.module('schemaForm').provider('schemaForm',
if (!(schema.oneOf || schema.anyOf || angular.isArray(types))) return;
var f = stdFormObj(name, schema, options);
f.type = 'formselect';
f.key = options.path;
var schemas = [];
// TODO: What if there are more than one of these keys in the same schema?
if (angular.isArray(types)) {
Expand Down