I have created a form with two modes: 'Create' and 'Edit'. Depending on the mode selected, specific div content is displayed in different areas of the layout. The content of the div includes an input field and validation messages related to that input.
Here's an example for better understanding:
View:
<div ng-app="myApp">
<div ng-controller="MyCtrl">
<form name="editForm">
<div ng-show='mode == "Edit"'>
<div class="form-group" ng-class="{true:'has-error',false:''}[editForm.FirstName.$valid === false && editForm.FirstName.$dirty]">
<input type="text" ng-model="foo" ng-maxlength="5" name="foo" required />
<div class="help-block" ng-messages="editForm.foo.$error" ng-if="editForm.foo.$dirty">
<p ng-message="required">Required</p>
<p ng-message="maxlength">Too long</p>
</div>
</div>
</div>
<div ng-show='mode == "Create"'>
<div class="form-group" ng-class="{true:'has-error',false:''}[edit.foo.$valid === false && edit.foo.$dirty]">
<input type="text" ng-model="foo" ng-maxlength="5" name="foo" required />
<div class="help-block" ng-messages="editForm.foo.$error" ng-if="editForm.foo.$dirty">
<p ng-message="required">Required</p>
<p ng-message="maxlength">Too long</p>
</div>
</div>
</div>
</form>
<div>
Mode: {{mode}} <br>
Value: {{foo}} <br>
Is form valid: {{editForm.foo.$valid}} <br>
</div>
</div>
</div>
Controller:
var myApp = angular.module('myApp',['ngMessages']);
myApp.controller('MyCtrl',function($scope){
$scope.mode = 'Edit';
});
Check out this JSFiddle for a working example.
The issue I'm facing is that the validation messages only display properly when the last input is shown. In Edit mode, even though it's the same input, the messages do not show correctly.