Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show multiple messages for different form fields using ng-messages

Is it possible to use ng-messages with angular 1.3 and show validation summary on the top with error messages for different fields?

<div ng-messages="myForm.$error">
    <div ng-message="required">Need to show required error for field 1.</div>
    <div ng-message="required">Need to show required error for field 2.</div>
</div>

How can i achieve this ? All the examples i have seen so far is showing error messages for one single field, not for all the fields in the form.

Do I need to have multiple ng-messages section for each field?

like image 791
user636525 Avatar asked Dec 24 '22 21:12

user636525


2 Answers

I wanted to style the whole summary block as one. So i ended up doing this.

<div class="error-summary" ng-show="myForm.$submitted && myForm.$invalid">
    <p ng-show="myForm.field1.$error.required">Field 1 is required.</p>
    <p ng-show="myForm.field2.$error.required">Field 2 is required.</p>
</div>

I was hoping to do something similar to this using ng-messages.

like image 57
user636525 Avatar answered May 12 '23 04:05

user636525


How about this?

<div ng-messages="myForm.field1.$error">
    <div ng-message="required">Need to show required error for field 1</div>
</div>
<div ng-messages="myForm.field2.$error">
    <div ng-message="required">Need to show required error for field 2</div>
</div>
<div ng-messages="myForm.field3.$error">
    <div ng-message="required">Need to show required error for field 3</div>
</div>
like image 33
pixelbits Avatar answered May 12 '23 04:05

pixelbits