angular-schema-form
angular-schema-form copied to clipboard
fix for #888 - array validation broken in angular 1.5.10
Description
This fixes issue #888. On angular 1.5.10+, firstDigest
value is false
on the initial load which triggers validation on empty fields. There might be some changes in the digest cycle/processing between angular v1.5.9 and v1.5.10+
firstDigest
is initialised in sf-schema-directive.js
// We need to know if we're in the first digest looping
// I.e. just rendered the form so we know not to validate
// empty fields.
childScope.firstDigest = true;
// We use a ordinary timeout since we don't need a digest after this.
setTimeout(function() {
childScope.firstDigest = false;
scope.$apply();
}, 0);
and consumed in sf-array-directive.js
in the following block:
// We need to have a ngModel to hook into validation. It doesn't really play well with
// arrays though so we both need to trigger validation and onChange.
// So we watch the value as well. But watching an array can be tricky. We wan't to know
// when it changes so we can validate,
let watchFn = function() {
// scope.modelArray = modelArray;
scope.modelArray = scope.$eval(attrs.sfNewArray);
// validateField method is exported by schema-validate
if (scope.ngModel && scope.ngModel.$pristine && scope.firstDigest &&
(!scope.options || scope.options.validateOnRender !== true)) {
return;
}
else if (scope.validateField) {
scope.validateField();
}
};
Tests were carried on angular 1.5.9 and 1.5.10 to check the $pristine
value of the field (schema-validatate-directive.js
):
ngModel.$formatters.push(function(val) {
// When a form first loads this will be called for each field.
// we usually don't want that.
if (ngModel.$pristine && first &&
(!scope.options || scope.options.validateOnRender !== true)) {
first = false;
return val;
}
validate(ngModel.$modelValue);
return val;
});
Angular 1.5.9
initial load page:
ngModel.$pristine true
click on yes radio button:
ngModel.$pristine true
ngModel.$pristine true
ngModel.$pristine true
ngModel.$pristine true
Angular 1.5.10
initial load page:
ngModel.$pristine true
click on yes radio button:
ngModel.$pristine true
ngModel.$pristine true
ngModel.$pristine false <----
ngModel.$pristine true
Removing scope.firstDigest
from the condition in (watchFn function - sf-array-directive.js
) line 22
fixes this issue.
Checklist
- [x] I have read and understand the CONTRIBUTIONS.md file
- [x] I have searched for and linked related issues
- [ ] I have created test cases to ensure quick resolution of the PR is easier
- [x] I am NOT targeting main branch
- [x] I did NOT include the dist folder in my PR
@json-schema-form/angular-schema-form-lead
Diff between AngularJS 1.5.9 and 1.5.10 https://github.com/angular/angular.js/compare/v1.5.9...v1.5.10
This PR appears to include private code changes. It should not be merged.