question / request for dynamic ng-messages error
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.
I suggest that this is unlikely to be done before 1.6.
:+1:
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.
+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 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, please open a separate issue if you think this is indeed a bug in Angular. This should work though.
+1