Skip to content

Commit

Permalink
Merge pull request #67 from Leopoldthecoder/master
Browse files Browse the repository at this point in the history
fix nestedSchema support
  • Loading branch information
furybean authored Aug 8, 2016
2 parents 0ec7fd2 + 37dfc83 commit 79fce37
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 8 deletions.
7 changes: 3 additions & 4 deletions src/form/fields/field-common.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,9 @@ export default {
onCreated() {
if (this.$parent.$isForm) {
this.form = this.$parent;
if (this.form && this.form.model) {
this.model = this.form.model;
}
}
},

Expand Down Expand Up @@ -177,10 +180,6 @@ export default {
if (!this._props.editorWidth.raw && form.editorWidth) {
this.editorWidth = form.editorWidth;
}

if (form && form.model) {
this.model = form.model;
}
}

if (this.model && this.model.$on) {
Expand Down
26 changes: 22 additions & 4 deletions src/schema/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { default as validatorFnMap } from './validators';
import { formatDate, merge, getPath } from '../util';
import { formatDate, merge, getPath, getNestedPath } from '../util';
import { default as defaultMessages } from './messages';

var doValidate = function(object, property, descriptor, rule) {
Expand All @@ -14,8 +14,9 @@ var doValidate = function(object, property, descriptor, rule) {
}

var clonedRule = merge({ message: message || '' }, rule);
let target = getPath(object, property);

if (!validateFn.call(object, object[property], clonedRule, property, descriptor)) {
if (!validateFn.call(object, target, clonedRule, property, descriptor)) {
object.$hints[property] = clonedRule.message;

return false;
Expand Down Expand Up @@ -210,6 +211,12 @@ class Schema {
}
}

for (let nestedSchema in this.nestedSchema) {
if (this.nestedSchema.hasOwnProperty(nestedSchema)) {
result[nestedSchema] = this.nestedSchema[nestedSchema].newModel();
}
}

initObject(result, this, true);

return result;
Expand All @@ -220,7 +227,13 @@ class Schema {
}

getPropertyLabel(property) {
return (this.props[property] || {}).label || '';
let target;
if (property.indexOf('.') > -1) {
target = getNestedPath(this.props, property);
} else {
target = this.props[property];
}
return (target || {}).label || '';
}

getPropertyMapping(property, object, ...args) {
Expand Down Expand Up @@ -334,7 +347,12 @@ class Schema {
initObject(object, this);

const props = this.props;
const descriptor = props[property];
let descriptor;
if (property.indexOf('.') > -1) {
descriptor = getNestedPath(props, property);
} else {
descriptor = props[property];
}

if (!descriptor) {
console.warn(`no property ${property} found in object:`, object); // eslint-disable-line no-console
Expand Down
6 changes: 6 additions & 0 deletions src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,12 @@ export function throttle(fn, delay) {
};
}

export function getNestedPath(object, nestedProp) {
let propertyArr = nestedProp.split('.');
let property = propertyArr.pop();
return getPath(object, propertyArr.join('.')).fields[property];
}

export function getPath(object, prop) {
prop = prop || '';
var paths = prop.split('.');
Expand Down

0 comments on commit 79fce37

Please sign in to comment.