Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

question / request for dynamic ng-messages error #9849

Open
chiefjester opened this issue Oct 30, 2014 · 27 comments
Open

question / request for dynamic ng-messages error #9849

chiefjester opened this issue Oct 30, 2014 · 27 comments

Comments

@chiefjester
Copy link

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.

@Narretz
Copy link
Contributor

Narretz commented Oct 30, 2014

Well, why don't you try it?

@chiefjester
Copy link
Author

any hints on how to traverse / get the field name of the enclosing scope?

@Narretz
Copy link
Contributor

Narretz commented Oct 31, 2014

$name is set on the ngModelController, so you can get it like myForm.myField.$name.

It's apparently not documented. :(

@Narretz Narretz added this to the Backlog milestone Oct 31, 2014
@Narretz Narretz self-assigned this Oct 31, 2014
@chiefjester
Copy link
Author

@Narretz, if I use it as a template, and use ng-message-include, how can I access the name of the field, (basically) not knowing the actual field?

<script type="text/ng-template" id="error-messages">
  <div ng-message="required">
    This {{ myForm.__what-to-place-here__.$name  }} is required
  </div>
</script>

@JeroMiya
Copy link

JeroMiya commented Jan 8, 2015

ngModelController.$name is not what you would typically show in an error message. Rather, you would use the label.

Other use cases:
ngMinlength, ngMaxLength: could (or do?) these directives save the min and max length somewhere on the ngModel controller (maybe the validator itself, at link time?), so that the ngMessage error can use them, e.g. "This value is less than the minimum length of {{parentForm[fieldId].$validators.minlength.value}}."

@adrianandreias
Copy link

This is similar to #10037. Is it currently achievable with 1.4.0-beta.5?

If there's currently no way to reference the field (actually I think "pass" the field because ng-messages-include and the field itself are not really linked in anyway, just they usually appear one after the other in DOM) it would be nice to have especially in the light of the new dynamic ng-messages: #10676

You can currently achieve this, but without reusing messages in ng-messages-include:

<input name="field1" type="number" ng-model="myForm.field1">
<div ng-messages="myForm.field1.$error">
  <span ng-message="backend">{{ backendErrors['field1'] }}</span>
  <ng-messages-include src="msgs.html"></ng-messages-include>
</div>

<input name="field2" type="number" ng-model="myForm.field2">
<div ng-messages="myForm.field2.$error">
  <span ng-message="backend">{{ backendErrors['field2'] }}</span>
  <ng-messages-include src="msgs.html"></ng-messages-include>
</div>

I assume a simple implementation would be to just pass the field object to ng-messages-include:

<ng-messages-include src="msgs.html" field="myForm.field1"></ng-messages-include>

And then just use use the field object in the msgs.html file:

<span ng-message="backend">{{ backendErrors[field.$name] }}</span>

And in controller you'd just manipulate these:

$scope.myForm.field1.$error = {'backend': true}
$scope.backendErrors['field1'] = 'Dynamic error'

@matsko what do you think? :)

@adrianandreias
Copy link

Any comment on this? The ng-messages-include dynamism is severely limited if there is no way to differentiate between form controls.

@bastijns-jeroen
Copy link

+1

@NBerghen
Copy link

+1 :)

@Alasiac
Copy link

Alasiac commented Apr 22, 2015

+1

7 similar comments
@ajhodges
Copy link

+1

@IvanJosipovic
Copy link

+1

@ctomoiaga
Copy link

+1

@jorgenbs
Copy link

+1

@thuey
Copy link

thuey commented Jul 2, 2015

+1

@mamartins
Copy link

+1

@siva-I
Copy link

siva-I commented Aug 26, 2015

+1

@antidote2
Copy link

+1

@matsko
Copy link
Contributor

matsko commented Oct 6, 2015

Alright this definitely seems super popular. I'm happy to add something like this in... It just needs to have planning first. @petebacondarwin and @IgorMinar what do you guys think?

@sureshvramani
Copy link

+1

@petebacondarwin
Copy link
Contributor

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

@pranaydutta89
Copy link

👍

@aliok
Copy link

aliok commented Dec 8, 2015

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.

@HichemBenChaaben
Copy link

+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>

@codymullins
Copy link

codymullins commented Jun 1, 2016

@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.

@gkalpak
Copy link
Member

gkalpak commented Jun 1, 2016

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

@george3447
Copy link

+1

@Narretz Narretz modified the milestones: 1.6.x, 1.7.x Apr 12, 2018
@petebacondarwin petebacondarwin self-assigned this May 14, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.