Skip to content

Commit 5836690

Browse files
committed
Added new feature, copyValueTo and updated sink and docs
1 parent 91aa366 commit 5836690

File tree

3 files changed

+30
-3
lines changed

3 files changed

+30
-3
lines changed

docs/index.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ Documentation
1414
1. [Validation Messages](#validation-messages)
1515
1. [Inline feedback icons](#inline-feedback-icons)
1616
1. [ngModelOptions](#ngmodeloptions)
17+
1. [copyValueTo](#copyvalueto)
1718
1. [Specific options and types](#specific-options-and-types)
1819
1. [fieldset and section](#fieldset-and-section)
1920
1. [conditional](#conditional)
@@ -342,6 +343,7 @@ General options most field types can handle:
342343
// and their items will inherit it.
343344
htmlClass: "street foobar", // CSS Class(es) to be added to the container div
344345
fieldHtmlClass: "street" // CSS Class(es) to be added to field input (or similar)
346+
copyValueTo: ["address.street"] // Copy values to these schema keys.
345347
}
346348
```
347349
@@ -458,6 +460,16 @@ Ex.
458460
See [Global Options](#global-options) for an example how you set entire form
459461
to validate on blur.
460462
463+
### copyValueTo
464+
This option has a very specific use case. Imagine you have the same option in several places, but you want them to be controlled from just one field. You specify what keys the value should be copied to, and the *viewValue* will be copied to these keys on the model. **Note: changing the model directly will not copy the value, it's intended for copying user input**. The recieving fields can be shown, but the intent for them is to be hidden.
465+
466+
Ex.
467+
```javascript
468+
{
469+
key: "email.main",
470+
copyValueTo: ["email.confirm", "other.email"]
471+
}
472+
```
461473
462474
Specific options and types
463475
--------------------------

examples/data/sink.json

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,14 @@
152152
{
153153
"title": "More stuff",
154154
"items": [
155-
"attributes",
155+
"attributes.eyecolor",
156+
"attributes.haircolor",
157+
{
158+
"key": "attributes.shoulders.left",
159+
"title": "Left shoulder",
160+
"description": "This value is copied to attributes.shoulders.right in the model",
161+
"copyValueTo": ["attributes.shoulders.right"]
162+
},
156163
{
157164
"key": "shoesize",
158165
"feedback": false

src/directives/schema-validate.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
angular.module('schemaForm').directive('schemaValidate', ['sfValidator', function(sfValidator) {
1+
angular.module('schemaForm').directive('schemaValidate', ['sfValidator', 'sfSelect', function(sfValidator, sfSelect) {
22
return {
33
restrict: 'A',
44
scope: false,
@@ -21,6 +21,15 @@ angular.module('schemaForm').directive('schemaValidate', ['sfValidator', functio
2121
};
2222
var form = getForm();
2323

24+
if (form.copyValueTo) {
25+
ngModel.$viewChangeListeners.push(function() {
26+
var paths = form.copyValueTo;
27+
angular.forEach(paths, function(path) {
28+
sfSelect(path, scope.model, ngModel.$viewValue);
29+
});
30+
});
31+
}
32+
2433
// Validate against the schema.
2534

2635
// Get in last of the parses so the parsed value has the correct type.
@@ -55,7 +64,6 @@ angular.module('schemaForm').directive('schemaValidate', ['sfValidator', functio
5564
});
5665
}
5766

58-
5967
// Listen to an event so we can validate the input on request
6068
scope.$on('schemaFormValidate', function() {
6169

0 commit comments

Comments
 (0)