Skip to content

Commit

Permalink
fix(Array) Array in property access (ngx-formly#343)
Browse files Browse the repository at this point in the history
  • Loading branch information
mohammedzamakhan authored Dec 15, 2016
1 parent 1ec5010 commit 16e1beb
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 14 deletions.
17 changes: 10 additions & 7 deletions demo/hello.ts
Original file line number Diff line number Diff line change
Expand Up @@ -360,12 +360,20 @@ export class HelloApp {
},
},
{
key: 'nested.arrays.0',
key: 'nested.arrays.0.item',
type: 'input',
templateOptions: {
label: 'Array with dots',
},
defaultValue: 'Default Value A',
parsers: [(value) => (value || '').toUpperCase()],
}, {
key: 'nested.arrays[1].item',
type: 'input',
templateOptions: {
label: 'Array property',
},
defaultValue: 'Default Value',
defaultValue: 'Default Value B',
parsers: [(value) => (value || '').toUpperCase()],
}, {
type: 'repeatSection',
Expand Down Expand Up @@ -433,11 +441,6 @@ export class HelloApp {
sports: false,
languages: true,
},
nested: {
property: {
magic: 'Nested property Content',
},
},
investments: [
{
investmentName: 'Formly',
Expand Down
11 changes: 6 additions & 5 deletions src/core/services/formly.form.builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { FormGroup, FormArray, FormControl, AbstractControl, Validators } from '
import { FormlyConfig } from './formly.config';
import { evalStringExpression, evalExpressionValueSetter, evalExpression, getFieldId, assignModelValue, isObject } from './../utils';
import { FormlyFieldConfig } from '../components/formly.field.config';
import { getKeyPath } from '../utils';

@Injectable()
export class FormlyFormBuilder {
Expand Down Expand Up @@ -49,17 +50,17 @@ export class FormlyFormBuilder {
if (field.defaultValue) {
this.defaultPath = path;
}
path = path.split('.');
path = getKeyPath({key: field.key});
}

if (path.length > 1) {
const rootPath = path.shift();
let nestedForm = <FormGroup>(form.get(rootPath) ? form.get(rootPath) : new FormGroup({}, field.validators ? field.validators.validation : undefined, field.asyncValidators ? field.asyncValidators.validation : undefined));
if (!form.get(rootPath)) {
let nestedForm = <FormGroup>(form.get(rootPath.toString()) ? form.get(rootPath.toString()) : new FormGroup({}, field.validators ? field.validators.validation : undefined, field.asyncValidators ? field.asyncValidators.validation : undefined));
if (!form.get(rootPath.toString())) {
form.addControl(rootPath, nestedForm);
}
if (!model[rootPath]) {
model[rootPath] = isNaN(rootPath) ? {} : [];
model[rootPath] = isNaN(path[0]) ? {} : [];
}

const originalKey = field.key;
Expand All @@ -71,7 +72,7 @@ export class FormlyFormBuilder {
this.formlyConfig.getMergedField(field);
this.addFormControl(form, field, model[path[0]] || field.defaultValue || '');
if (field.defaultValue && !model[path[0]]) {
let path = this.defaultPath.split('.');
let path: any = getKeyPath({key: this.defaultPath});
path = path.pop();
assignModelValue(this.model, path, field.defaultValue);
this.defaultPath = undefined;
Expand Down
4 changes: 2 additions & 2 deletions src/core/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export function getFieldModel(model: any, field: FormlyFieldConfig, constructEmp

export function assignModelValue(model, path, value) {
if (typeof path === 'string') {
path = path.split('.');
path = getKeyPath({key: path});
}

if (path.length > 1) {
Expand All @@ -82,7 +82,7 @@ export function assignModelValue(model, path, value) {

export function getValueForKey(model, path) {
if (typeof path === 'string') {
path = path.split('.');
path = getKeyPath({key: path});
}
if (path.length > 1) {
const e = path.shift();
Expand Down

0 comments on commit 16e1beb

Please sign in to comment.