I'm creating a web application using AngularJS, and i have a form with two fields that should be validated as mutually exclusive. I mean either one or the other field should be filled, but not both (it is also valid if neither is filled). My form (simplified model of the real case) is like this:
<form id="form1" name="form1">
<p>
<input type="text" id="field1" name="field1" ng-model="model.item1" not-both-filled="model.item2" />
</p>
<p>
<input type="text" id="field2" name="field2" ng-model="model.item2" not-both-filled="model.item1" />
</p>
<p ng-show="form1.$invalid">not valid</p>
<p ng-show="!form1.$invalid">valid</p>
</form>
and i created a directive like this:
angular.module('myApp', []).directive('notBothFilled', function() {
return {
require: 'ngModel',
link: function(scope, elem, attrs, ctrl) {
var validateNotBothFilled = function(value) {
var theOther = scope.$eval(attrs.notBothFilled);
var isNotValid = (value && value !== '' && theOther && theOther !== '');
ctrl.$setValidity('field1', !isNotValid);
ctrl.$setValidity('field2', !isNotValid);
return value;
};
ctrl.$parsers.unshift(validateNotBothFilled);
ctrl.$formatters.unshift(validateNotBothFilled);
}
};
});
However, this has the following problem:
- fill field 1
- fill field 2
- form becomes invalid
- empty field 1
- form should become valid but does not
If i empty field2 instead, the behaviour is correct.
Sorry i still have very little AngularJS experience (and Englis is not my native language), but could someone tell me:
- Is directive like this is generally the correct approach to validate mutually exclusive fields in AngularJS?
- What is wrong with my code, how can i get the form valid again if i empty any of the fields?
I also have a JS fiddle: http://jsfiddle.net/k6ps/7vCZg/
Sincerely, k6ps