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.
Well, why don't you try it?
any hints on how to traverse / get the field name of the enclosing scope?
$name is set on the ngModelController, so you can get it like myForm.myField.$name.
It's apparently not documented. :(
@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>
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}}."
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? :)
Any comment on this? The ng-messages-include dynamism is severely limited if there is no way to differentiate between form controls.
+1
+1 :)
+1
+1
+1
+1
+1
+1
+1
+1
+1
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?
+1