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

Well, why don't you try it?

Narretz avatar Oct 30 '14 20:10 Narretz

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

chiefjester avatar Oct 31 '14 10:10 chiefjester

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

It's apparently not documented. :(

Narretz avatar Oct 31 '14 19:10 Narretz

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

chiefjester avatar Nov 01 '14 21:11 chiefjester

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}}."

JeroMiya avatar Jan 08 '15 19:01 JeroMiya

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 avatar Mar 02 '15 11:03 adrianandreias

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

adrianandreias avatar Apr 03 '15 17:04 adrianandreias

+1

bastijns-jeroen avatar Apr 22 '15 07:04 bastijns-jeroen

+1 :)

NBerghen avatar Apr 22 '15 07:04 NBerghen

+1

Alasiac avatar Apr 22 '15 08:04 Alasiac

+1

ajhodges avatar Apr 23 '15 16:04 ajhodges

+1

IvanJosipovic avatar Apr 28 '15 19:04 IvanJosipovic

+1

ctomoiaga avatar May 20 '15 10:05 ctomoiaga

+1

jorgenbs avatar Jun 17 '15 11:06 jorgenbs

+1

thuey avatar Jul 02 '15 17:07 thuey

+1

mamartins avatar Aug 10 '15 22:08 mamartins

+1

siva-I avatar Aug 26 '15 21:08 siva-I

+1

antidote2 avatar Oct 06 '15 11:10 antidote2

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?

matsko avatar Oct 06 '15 16:10 matsko

+1

sureshvramani avatar Oct 08 '15 15:10 sureshvramani