For example I have the form where I am showing form input errors.
I need to show red badge (with 'hover to show errors') near input label if there are some errors. If user will hover this red badge - he will see list of errors using AngularJS UI Bootstrap tooltip. I don't want to put list of errors into tooltip-html-unsafe attribute, because it is not convenient to edit and maintain.
This code is more declarative:
<validation-tooltip ng-show="appForm.number.$invalid && appForm.number.$dirty">
<ul>
<li ng-show="appForm.number.$error.required">this field is required</li>
<li ng-show="appForm.number.$error.number">should be number</li>
<li ng-show="appForm.number.$error.min">minimum - 5</li>
<li ng-show="appForm.number.$error.max">miximum - 20</li>
</ul>
</validation-tooltip>
than this code:
<span tooltip-html-unsafe="{{<ul><li>This field is required;</li><li>...</li></ul>}}">hover to show errors</span>
How can I write such validation-tooltip directive using AngularJS UI Bootstrap tooltip?
Or maybe can you suggest another approach to maintain validation error messages?
My goal was to leverage both ng-messages and ui-bootstrap popover for validation feedback. I prefer the popover vs. the tooltip as it displays the help-block styles more clearly.
Here's the code:
Here is the validationMessages.html
Note: I had to upgrade to jQuery 2.1.4 to get the uib-popover-template directive to work.
Dependencies:
Demo Fiddle
Validation Tooltip Directive
The validationTooltip is the main directive. It has the following responsibilities:
Additional Notes
The tooltip template uses the transclusion function from the link function to bind the template to the directive's scope. There are two properties that are within scope that the template can bind to:
This allows the template to bind to validation properties such as $valid, $invalid, $pristine, $dirty, and $error without referring to the form name or the input field's name directly. For example, all of the following expressions are valid binding expressions:
$form properties:
$field properties:
Directive Implementation
Validation Message Directive
The validationMessage directive keeps track of the validation messages to display in the tooltip. It uses
ng-if
to define the expression to evaluate. If there is nong-if
found on the element, then the expression simply evaluates to true (always shown).Usage in HTML
you can actually just use the tooltip-enable property:
@pixelbits answer is great. I used this instead:
The technique is ui-bootstrap's tooltip and set the tooltip text to '' when valid.
http://jsbin.com/ditekuvipa/2/edit
Great answer from @pixelbits. I used his directives and modified them slightly to allow the tooltip to display over the actual input as some users requested.
The major change is that the directive uses jQuery to find the target element (which should be an input) via the
name
attribute, and initializes the tooltip on that element rather than the element of the directive. I also added ashowWhen
property to the scope as you may not always want your tooltip to show when the input is invalid (see example below).The validationMessage directive is unchanged
Usage in Html is also similar, with just the addition of
showWhen
if you want: