Skip to content
This repository was archived by the owner on Apr 30, 2018. It is now read-only.

Commit 55a915a

Browse files
author
Kent C. Dodds
committed
Merge pull request #572 from MCKRUZ/master
fix(formly-field): Breaking change in @7.3.2? Key value? #566
2 parents 917ec26 + 1db33a9 commit 55a915a

File tree

2 files changed

+73
-3
lines changed

2 files changed

+73
-3
lines changed

src/directives/formly-field.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ function formlyField($http, $q, $compile, $templateCache, $interpolate, formlyCo
3535

3636
// @ngInject
3737
function FormlyFieldController($scope, $timeout, $parse, $controller, formlyValidationMessages) {
38-
/* eslint max-statements:[2, 31] */
38+
/* eslint max-statements:[2, 32] */
3939
if ($scope.options.fieldGroup) {
4040
setupFieldGroup()
4141
return
@@ -82,13 +82,17 @@ function formlyField($http, $q, $compile, $templateCache, $interpolate, formlyCo
8282
return parseGet($scope.options.key, $scope.model)
8383
}
8484

85+
function shouldNotUseParseKey(key) {
86+
return angular.isNumber(key) || (/^\d/.test(key) && key.indexOf('[') === -1)
87+
}
88+
8589
function parseSet(key, model, newVal) {
8690
// If either of these are null/undefined then just return undefined
8791
if (!key || !model) {
8892
return
8993
}
9094
// If we are working with a number then $parse wont work, default back to the old way for now
91-
if (angular.isNumber(key)) {
95+
if (shouldNotUseParseKey(key)) {
9296
// TODO: Fix this so we can get several levels instead of just one with properties that are numeric
9397
model[key] = newVal
9498
} else {
@@ -106,7 +110,7 @@ function formlyField($http, $q, $compile, $templateCache, $interpolate, formlyCo
106110
}
107111

108112
// If we are working with a number then $parse wont work, default back to the old way for now
109-
if (angular.isNumber(key)) {
113+
if (shouldNotUseParseKey(key)) {
110114
// TODO: Fix this so we can get several levels instead of just one with properties that are numeric
111115
return model[key]
112116
} else {

src/directives/formly-field.test.js

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -519,6 +519,72 @@ describe('formly-field', function() {
519519
})
520520
})
521521

522+
describe('getterSetters', () => {
523+
it('should get and set values when key is all alpha', () => {
524+
const key = 'foo'
525+
const defaultValue = 'bar'
526+
527+
scope.fields = [
528+
{template: input, key, defaultValue},
529+
]
530+
scope.model = {}
531+
532+
compileAndDigest()
533+
expect(scope.model[key]).to.eq(defaultValue)
534+
})
535+
536+
it('should get and set values when key is all numeric', () => {
537+
const key = '1333'
538+
const defaultValue = 'bar'
539+
540+
scope.fields = [
541+
{template: input, key, defaultValue},
542+
]
543+
scope.model = {}
544+
545+
compileAndDigest()
546+
expect(scope.model[key]).to.eq(defaultValue)
547+
})
548+
549+
it('should get and set values when key is alpha numeric with alpha first', () => {
550+
const key = 'A1'
551+
const defaultValue = 'bar'
552+
553+
scope.fields = [
554+
{template: input, key, defaultValue},
555+
]
556+
scope.model = {}
557+
558+
compileAndDigest()
559+
expect(scope.model[key]).to.eq(defaultValue)
560+
})
561+
562+
it('should get and set values when key is alpha numeric with numeric first', () => {
563+
const key = '1A'
564+
const defaultValue = 'bar'
565+
566+
scope.fields = [
567+
{template: input, key, defaultValue},
568+
]
569+
scope.model = {}
570+
571+
compileAndDigest()
572+
expect(scope.model[key]).to.eq(defaultValue)
573+
})
574+
575+
it('should work with nested keys with numbers in the key', () => {
576+
const key = 'foo3bar.baz4foobar'
577+
const defaultValue = 'baz'
578+
scope.fields = [
579+
{template: input, key, defaultValue},
580+
]
581+
scope.model = {}
582+
583+
compileAndDigest()
584+
expect(scope.model.foo3bar.baz4foobar).to.equal(defaultValue)
585+
})
586+
})
587+
522588
describe(`id property`, () => {
523589
it(`should default to a semi-random id that you cannot rely on and don't have to think about`, () => {
524590
scope.fields = [getNewField()]

0 commit comments

Comments
 (0)