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

Commit 4c526c5

Browse files
author
Kent C. Dodds
committed
Adding support for field.extras.validateOnModelChange
1 parent d5be310 commit 4c526c5

File tree

5 files changed

+56
-1
lines changed

5 files changed

+56
-1
lines changed

CHANGELOG.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
# 6.22.0
2+
3+
## New Features
4+
5+
- Adding `extras` property to the field configuration object (for configuration of angular-formly core features).
6+
- Adding support for `field.extras.validateOnModelChange` which will invoke the field's `formControl.$validate` function when the model or formState changes. [#404](/../../issues/404)
7+
8+
## Bug Fixes
9+
10+
- `hideExpressions` now run on both the field's own model change (if present) as well as the form's model's change and formState change. Thanks [@redhead](https://github.com/redhead)! [#380](/../../issues/380)
11+
12+
## Internal enhancements
13+
14+
- Adding test for the presence all the things that get added to the field configuration object
15+
- Adding test for the presence all the things that should be available on the scope of a formly-field (and hence available to template authors).
16+
117
# 6.21.1
218

319
## Bug Fixes

src/directives/formly-field.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ function formlyField($http, $q, $compile, $templateCache, $interpolate, formlyCo
8484
function simplifyLife(options) {
8585
// add a few empty objects (if they don't already exist) so you don't have to undefined check everywhere
8686
formlyUtil.reverseDeepMerge(options, {
87+
extras: {},
8788
data: {},
8889
templateOptions: {},
8990
validation: {}

src/directives/formly-field.test.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1569,6 +1569,19 @@ describe('formly-field', function() {
15691569
});
15701570
});
15711571

1572+
describe(`extras`, () => {
1573+
describe(`validateOnModelChange`, () => {
1574+
it(`should invoke $validate on the field even when the field's model hasn't changed`, () => {
1575+
scope.fields = [getNewField({extras: {validateOnModelChange: true}})];
1576+
compileAndDigest();
1577+
const $validateSpy = sinon.spy(field.formControl, '$validate');
1578+
scope.model.foo = 'bar';
1579+
scope.$digest();
1580+
expect($validateSpy).to.have.been.calledOnce;
1581+
});
1582+
});
1583+
});
1584+
15721585
describe(`other things`, () => {
15731586
it(`should warn if you specify 'hide' in expressionProperties`, inject(($log) => {
15741587
scope.fields = [getNewField({expressionProperties: {hide: 'foo'}})];
@@ -1581,6 +1594,26 @@ describe('formly-field', function() {
15811594
);
15821595
expect(log[2]).to.equal(field);
15831596
}));
1597+
1598+
it(`should add a bunch of things to the formly field and it's scope`, () => {
1599+
scope.fields = [{template: '<input ng-model="model[options.key]" />'}];
1600+
compileAndDigest();
1601+
// here's a list of everything that angular-formly adds for you.
1602+
expect(field).to.contain.all.keys([
1603+
'key', 'extras', 'data', 'templateOptions', 'validation', 'value', 'runExpressions',
1604+
'resetModel', 'updateInitialValue', 'id', 'name', 'initialValue', 'formControl'
1605+
]);
1606+
});
1607+
1608+
it(`should add a bunch of things to the formly field and it's scope`, () => {
1609+
scope.fields = [{template: '<input ng-model="model[options.key]" />'}];
1610+
compileAndDigest();
1611+
// here's a list of everything that you have available on the scope for your templates
1612+
expect(isolateScope).to.contain.all.keys([
1613+
'options', 'model', 'formId', 'index', 'fields', 'formState', 'formOptions',
1614+
'form', 'id', 'to', 'fc', 'name', 'showError'
1615+
]);
1616+
});
15841617
});
15851618

15861619
describe(`merging of options`, () => {

src/directives/formly-form.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,13 +118,15 @@ function formlyForm(formlyUsability, formlyWarn, $parse, formlyConfig, $interpol
118118

119119
function onModelOrFormStateChange() {
120120
angular.forEach($scope.fields, function runFieldExpressionProperties(field, index) {
121-
/*jshint -W030 */
122121
const model = field.model || $scope.model;
123122
field.runExpressions && field.runExpressions();
124123
if (field.hideExpression) { // can't use hide with expressionProperties reliably
125124
const val = model[field.key];
126125
field.hide = evalCloseToFormlyExpression(field.hideExpression, val, field, index);
127126
}
127+
if (field.extras && field.extras.validateOnModelChange && field.formControl) {
128+
field.formControl.$validate();
129+
}
128130
});
129131
}
130132

src/providers/formlyApiCheck.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,9 @@ const fieldOptionsApiShape = {
102102
id: apiCheck.string.optional,
103103
name: apiCheck.string.optional,
104104
expressionProperties: expressionProperties.optional,
105+
extras: apiCheck.shape({
106+
validateOnModelChange: apiCheck.bool.optional
107+
}).strict.optional,
105108
data: apiCheck.object.optional,
106109
templateOptions: apiCheck.object.optional,
107110
wrapper: specifyWrapperType.optional,

0 commit comments

Comments
 (0)