angular-validation icon indicating copy to clipboard operation
angular-validation copied to clipboard

Validators not updating when dependant on other form values

Open codeuniquely opened this issue 7 years ago • 2 comments

I have set up a 'plnker' to demonstrate a common issue when users fill in forms, that have values later on that are dependant on and validated by values put into previous inputs. Please see the following: https://plnkr.co/edit/ELplnBCAUjatpusW6EPU?p=preview

Enter some values - for example

10
Hello World
15

and you will get the warning AFTER the second Number of - 'A maximum value of 10 is allowed.'

If you now tab back UP the form or just click into the first input and enter a value of 100 instead and blur out of that control into the next one - then the value in form.value1 is updated. However the validation does not see the update and still continues to show an error messages - even hough its input is now actually valid (and its label and validator have both been updated) ....

How do you get the validation to fire again for the seconds input so that the error message goes away ... ?????

codeuniquely avatar Jul 04 '17 10:07 codeuniquely

I think there is a fix for this

There is already an $observe on the validator - which updates it and we could use this to re-run the validation rule after...

current code

/**
  * Observe validator changes in order to allow dynamically change it
  */
  attrs.$observe('validator', function(value) {
  validation = value.split(',');
});

if this was updated to also make a call back to checkValidation then the error messages would automatically be removed once the conditions on the later validation have been met

Suggestion:

/**
  * Observe validator changes in order to allow dynamically change it
  */
  attrs.$observe('validator', function(value) {
  validation = value.split(',');
  // validation has been updated => make the validation fire again
  var theValue = useViewValue ? ctrl.$viewValue : ctrl.$modelValue;
  checkValidation(scope, element, attrs, ctrl, validation, theValue);
});

codeuniquely avatar Jul 04 '17 13:07 codeuniquely

It appears to work - in my code - does this effect / break anything else ?

codeuniquely avatar Jul 04 '17 13:07 codeuniquely