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

Commit 460ead5

Browse files
feat(formly-form): validate field after expressions are run
As runExpressions happens on NextTick return a promise and then validate. If there is no promise, just validate straight away
1 parent b55eb77 commit 460ead5

File tree

3 files changed

+9
-3
lines changed

3 files changed

+9
-3
lines changed

src/directives/formly-field.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ function formlyField($http, $q, $compile, $templateCache, $interpolate, formlyCo
5959
// function definitions
6060
function runExpressions() {
6161
// must run on next tick to make sure that the current value is correct.
62-
$timeout(function runExpressionsOnNextTick() {
62+
return $timeout(function runExpressionsOnNextTick() {
6363
const field = $scope.options;
6464
const currentValue = valueGetterSetter();
6565
angular.forEach(field.expressionProperties, function runExpression(expression, prop) {

src/directives/formly-field.test.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1690,6 +1690,7 @@ describe('formly-field', function() {
16901690
const $validateSpy = sinon.spy(field.formControl, '$validate');
16911691
scope.model.foo = 'bar';
16921692
scope.$digest();
1693+
$timeout.flush();
16931694
expect($validateSpy).to.have.been.calledOnce;
16941695
});
16951696
});

src/directives/formly-form.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,13 +120,18 @@ function formlyForm(formlyUsability, formlyWarn, $parse, formlyConfig, $interpol
120120
function onModelOrFormStateChange() {
121121
angular.forEach($scope.fields, function runFieldExpressionProperties(field, index) {
122122
const model = field.model || $scope.model;
123-
field.runExpressions && field.runExpressions();
123+
const promise = field.runExpressions && field.runExpressions();
124124
if (field.hideExpression) { // can't use hide with expressionProperties reliably
125125
const val = model[field.key];
126126
field.hide = evalCloseToFormlyExpression(field.hideExpression, val, field, index);
127127
}
128128
if (field.extras && field.extras.validateOnModelChange && field.formControl) {
129-
field.formControl.$validate();
129+
const validate = field.formControl.$validate;
130+
if (promise) {
131+
promise.then(validate);
132+
} else {
133+
validate();
134+
}
130135
}
131136
});
132137
}

0 commit comments

Comments
 (0)