angular.js icon indicating copy to clipboard operation
angular.js copied to clipboard

question / request for dynamic ng-messages error

Open chiefjester opened this issue 12 years ago • 27 comments

I've been reading ng-messages api, and it seems very powerful. However I just want to ask if its possible to have the error message being dynamic.

Instead of:

<script type="text/ng-template" id="error-messages">
<div ng-message="required">This field is required</div>
<div ng-message="minlength">This field is too short</div>
</script>
<div ng-messages="myForm.myField.$error" ng-messages-include="error-messages"></div>

Is this possible?

<script type="text/ng-template" id="error-messages">
<div ng-message="required">This {{ $parent.field_name }} is required</div>
<div ng-message="minlength">This  {{ $parent.placeholder_perhaps }}  is too short</div>
</script>
<div ng-messages="myForm.myField.$error" ng-messages-include="error-messages"></div>

I believe doing it like this will make our code more DRY.

chiefjester avatar Oct 30 '14 18:10 chiefjester

I suggest that this is unlikely to be done before 1.6.

petebacondarwin avatar Oct 08 '15 18:10 petebacondarwin

:+1:

pranaydutta89 avatar Oct 27 '15 12:10 pranaydutta89

The solution should also solve the problem where I want to show the min allowed value in the error message for a input type="number". A message like:

"{{field.$name}} has a value less than allowed minimum value {{field.$something}}".

That property $something which is be the min attribute defined on the input element.

aliok avatar Dec 08 '15 13:12 aliok

+1 would be nice to have.

Just for info, it currently works with the version 1.5 of angular and ng-messages.

This is just to illustrate, (assuming that the inputs are within an ng-repeat) it works fine with the $index and the formname['fieldname'+$index].$error passed to ng-messages

<input name="name="firstname{{index}}" ng-required ng-minlength="3" ng-maxlength="25" />
<div class="help-block" ng-messages="formname['firstname'+$index].$error">
  <p ng-messages-include="components/partials/form-errors.html"></p>
</div>

HichemBenChaaben avatar Dec 08 '15 15:12 HichemBenChaaben

@HichemBenChaaben we just upgraded from Angular 1.4 to 1.5 based on that but we still aren't having any luck - looks like in the DOM the ng-messages isn't evaluating the $index to a number.

codymullins avatar Jun 01 '16 16:06 codymullins

@codymullins, please open a separate issue if you think this is indeed a bug in Angular. This should work though.

gkalpak avatar Jun 01 '16 17:06 gkalpak

+1

george3447 avatar Sep 08 '16 05:09 george3447