JSFiddle - React, Tailwind, and code Playground
HTML
<script src="http://code.jquery.com/jquery-1.8.2.js"></script>
<form ng-app="ai" ng-controller="ConfigCostsPreCalc" name="form">
<h2 class="span6">Required message is not hidden</h2>
Steps to reproduce the problem
<ul>
<li>add a line -> the contained input is empty which makes the form invalid,
the required message is shown</li>
<li>remove the line -> the form should be valid now, but the error message
is not hidden</li>
</ul>
<h2 class="span1">
<button type="button" ng-click="addYear()" class="btn">+</button>
</h2>
<div class="control-group error regular-line-height"> <span class="help-block" show="showErrorForRepeatedItem(form.$error.required, 'year')"
watch="[years, form.$error.required]">Required message</span>
</div>
<ul class="unstyled numberInput" name="years">
<li ng-repeat="year in years" class="row-fluid"> <span class="span6">
<input type="text" name="year" ng-model="year.year" required class="input-mini"><span> Years</span>
</span>
<span
class="span1">
<button type="button" ng-click="removeYears(year)" class="btn">-</button>
</span>
</li>
</ul>
</form>
JavaScript
var appAi = angular.module('ai', [])
appAi.directive("show", ["$parse", function factory($parse) {
return function (scope, element, attr) {
var watchExprs = attr.watch ? attr.watch : attr.show
scope.$watch(watchExprs, function watchAction() {
var predicate = $parse(attr.show);
element.css('display', predicate(scope) ? '' : 'none');
}, true);
};
}]);
appAi.controller('ConfigCostsPreCalc', ["$scope", function ($scope) {
$scope.years = [{
"year": "2"
}];
$scope.addYear = function () {
$scope.years.push({});
}
$scope.removeYears = function (item) {
var index = $.inArray(item, $scope.years)
$scope.years.splice(index, 1); //remove element
}
$scope.showErrorForRepeatedItem = function (violatingInputs, inputExpression) {
if (!violatingInputs) {
return false
}
var regex = new RegExp(inputExpression);
var hits = $.grep(violatingInputs,
function (el, index) {
return regex.test(el.$name);
}).length;
return hits > 0;
}
}]);