In some applications it can be really useful to have controllers listen for validation changes especially in more complicated AngularJS apps where ‘ng-repeat’ is used to render form controls. There’s plenty of cases where a parent scope might need to know about validation changes based on child scopes… one such case is a validation summary. There’s a couple ways to implement this (and probably more) but they all seem a bit hacky such as:
- Apply a $watch to the current form object’s $valid property in the parent scope, then use jQuery to look for elements that have a class like ‘invalid’
- You could then use the scope() function on the DOM element that ng-repeat is used on to get any model information about the invalid item
- In child scopes you could apply a $watch to individual form elements’ $valid property then change the $parent scope’s model values to indicate validation changes
Instead what I wanted to achieve was a re-usable way to ‘bubble’ up validation changes from any scope’s form element to ancestor scopes without having to do any of the following:
- No jquery DOM selection
- No hard coding of form names to access the validation objects
- No requirement to modifying other scopes’ values
The way I went about this was to create a very simple custom directive which I’ve called ‘val-bubble’ since it has to do with validation and it ‘bubbles’ up a message to any listening scopes. An input element might then look like this:
Then in an outer scope I can then listen for validation changes and do whatever I want with the result:
The args object contains these properties:
- isValid = is the field valid
- ctrl = the current form controller object for the field
- scope = the scope bound to the field being validated
- element = the DOM element of the field being validated
- expression = the current $watch expression used to watch this fields validation changes
With all of that information you can easily adds some additional functionality to your app based on the current validating inputs such as a validation summary or whatever.
The val-bubble custom directive is pretty simple, here’s the code and an explanation below:
The first thing we’re doing here is limiting this directive to be used only as an attribute and ensuring the element has a model applied to it. Then we make sure that the element has a ‘name’ value applied. After that we are getting a reference to the current form object that this field is contained within using a custom method: formHelper.getCurrentForm … more on this below. Lastly we are applying a $watch to the current element’s $valid property and when this value changes we $emit an event upwards to parent/ancestor scopes to listen for.
Above I mentioned that I wanted a re-usable solution where I didn’t need to hard code things like the current form name. Unfortunately Angular doesn’t really provide a way to do this OOTB (as far as I can tell!) (Update! see here on how to access the current form: http://shazwazza.com/post/Reference-the-current-form-controller-in-AngularJS) so I’ve just created a simple factory object that finds the current form object applied to the current scope. The type check is fairly rudimentary but it works, it’s simply checking each property that exists on the scope object and tries to detect the object that matches the definition of an Angular form object:
NOTE: the above code has a dependency on UnderscoreJS
So now you can just apply the val-bubble attribute to any input element to ensure it’s validation changes are published to listening scopes!