jonsamwell/angular-auto-validate

Fetching error messages from the error message resolver in a controller

Closed this issue · 3 comments

Hi !

First of all happy new year and I'd like to say it's an amazing stuff you've got right there !
I'm kinda new to Angular but I'm hoping what I'm asking will not be trivial or not too stupid...

Is there a way I can fetch the errorMsg which was set in a custom error resolver inside the angular controller after submitting the form ? The idea is basically to be able to display an alert popup displaying the first error message of the form (for instance by following the natural order of fields and their respective validation rules in the DOM).

Thanks in advance and cheers

hmmm, not currently. I would suggest doing this with a custom dom modifier that basically uses the default one (bs3) and surfaced another method to get the list of invalid messages.

http://jonsamwell.github.io/angular-auto-validate/#custom_dom_modifiers

I've figured something out, pretty sure it's not optimal though... I'm basically storing an errorMsg in my custom Element Modifier, which returns the first validation error of a given form.
This method is exposed and is made accessible from the controller, and it works even if there are several forms validated in a controller. Here is a gist if someone is interested https://gist.github.com/wrousseau/45f83e77d29a8eb48589

Not quite sure how to test that though. Could you tell me what triggers the validation pipeline (which begins with the error message resolver if I'm correct ?) whenever a field is changed for instance ?

Good work - this is exactly what I was thinking. Testing this would be like testing a normal service. I would suggest testing this in isolation from the pipeline as it makes for better atomic tests. However, you are correct the validation is triggered when the model property value is updated which (if invalid) triggers the message resolver which gives the message to the element modifier.

You can look at how I've tested the bootstrap3 element modifier (although you can just ignore all the DOM manipulation stuff) https://github.com/jonsamwell/angular-auto-validate/blob/master/tests/services/bootstrap3ElementModifier.spec.js