jonsamwell/angular-auto-validate

attempted form submission followed by a form reset and data restoration causes unwanted error condition

Closed this issue · 7 comments

I have a form with validate-non-visible-controls set. I populate the model and render the form. Validations fire and angular-auto-validate assesses them. At this point ctrlFormOptions.forceValidation is false in the validateForm method.

I then attempt to save my form. validateForm calls it's own processElement passing true for the parameter forceValidation. This value is set permanently on ctrlFormOptions. Errors appear as desired.

I then reset the form (using form.$setPristine like your method does). This clears the 'has error' class. I add the default values which kicks off validations as before. As soon as the validation fishes my fields turn red and the 'has error' class is added to the parent form-group.

I notice this behavior is specific to form submission. If I create the form, edit a field triggering an error condition, then 'reset' the subform by calling setPristine and repopulating the fields everything works as expected.

I believe the issue is related to forceValidation being set on model options in the submit method and never cleared.

On a side note is there a way to programatically trigger reset? I have sub forms that can be enabled/disabled. Disabling restores the form to it's initial state. This action is triggered by a button that is not withing the form itself.

Thanks!

Sorry I'm being a bit slow at responding to error at the moment I'm snowed under. Feel free to send a PR in to fix this issue. The only way I can think of resetting the form is by getting the form as the library need the individual element to change it ui.

Same here. The work around is pretty simple. I'll try to dive deeper and do a pull request sometime in the next few weekends.

Thanks @pjwalmsley that would be awesome!

+1

@pjwalmsley see #82 for a way to programatically trigger a form reset.

@pjwalmsley reseting the form using the one of the methods described in #82 might actually solve this problem please let me know.

I've now made sure the the original force value in the validation options is set after the validation so hopefully this error is fixed (https://github.com/jonsamwell/angular-auto-validate/blob/master/src/services/validationManager.js#L157). See latest release https://github.com/jonsamwell/angular-auto-validate/releases/tag/v1.18.9