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

Commit c03b2dc

Browse files
committed
fix(formly-form): validateOnModelChange now copes when field.formControl is an array
Run $validate() on each formControl issue #523
1 parent eae422f commit c03b2dc

File tree

2 files changed

+39
-4
lines changed

2 files changed

+39
-4
lines changed

src/directives/formly-field.test.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1934,6 +1934,31 @@ describe('formly-field', function() {
19341934
expect($validateSpy).to.have.been.calledOnce
19351935
})
19361936

1937+
it(`should cope when field.formControl has been upgraded to an array`, () => {
1938+
scope.model = {
1939+
multiNgModel: {
1940+
start: 'start',
1941+
stop: 'stop',
1942+
},
1943+
}
1944+
const field = getNewField({
1945+
key: 'multiNgModel',
1946+
template: multiNgModelField,
1947+
extras: {
1948+
validateOnModelChange: true,
1949+
},
1950+
})
1951+
scope.fields = [field]
1952+
compileAndDigest()
1953+
const $validateSpy0 = sinon.spy(field.formControl[0], '$validate')
1954+
const $validateSpy1 = sinon.spy(field.formControl[1], '$validate')
1955+
scope.model.foo = 'bar'
1956+
scope.$digest()
1957+
$timeout.flush()
1958+
expect($validateSpy0).to.have.been.calledOnce
1959+
expect($validateSpy1).to.have.been.calledOnce
1960+
})
1961+
19371962
it.skip(`should run field expressions when form is initialised`, () => {
19381963
scope.model = {email: ''}
19391964
scope.fields = [getNewField({

src/directives/formly-form.js

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,15 @@ function formlyForm(formlyUsability, formlyWarn, $parse, formlyConfig, $interpol
126126
angular.forEach($scope.fields, runFieldExpressionProperties)
127127
}
128128

129+
function validateFormControl(formControl, promise) {
130+
const validate = formControl.$validate
131+
if (promise) {
132+
promise.then(validate)
133+
} else {
134+
validate()
135+
}
136+
}
137+
129138
function runFieldExpressionProperties(field, index) {
130139
const model = field.model || $scope.model
131140
const promise = field.runExpressions && field.runExpressions()
@@ -134,11 +143,12 @@ function formlyForm(formlyUsability, formlyWarn, $parse, formlyConfig, $interpol
134143
field.hide = evalCloseToFormlyExpression(field.hideExpression, val, field, index)
135144
}
136145
if (field.extras && field.extras.validateOnModelChange && field.formControl) {
137-
const validate = field.formControl.$validate
138-
if (promise) {
139-
promise.then(validate)
146+
if (angular.isArray(field.formControl)) {
147+
angular.forEach(field.formControl, function(formControl) {
148+
validateFormControl(formControl, promise)
149+
})
140150
} else {
141-
validate()
151+
validateFormControl(field.formControl, promise)
142152
}
143153
}
144154
}

0 commit comments

Comments
 (0)