With a simple form:
<form name="simpleForm">
<input ng-model= "profile.cat" name="cat">
<input ng-model= "profile.dog" name="dog">
<input ng-model= "profile.mouse" name="mouse">
</form>
What is the simplest way to validate that all three inputs have been filled or left empty? In other words, the form is valid only if all three inputs are filled or all three inputs are empty. I've made several attempts at a custom validator, all along the lines of putting a directive inside one or more of the three inputs, like:
<input ng-model= "profile.cat" name="cat" all-empty-all-full >
custom validator:
function allEmptyAllFull() {
return {
restrict: 'A',
require: 'ngModel',
link: function (scope, element, attrs, ngModel) {
// nothing I put in here does the job!
}
}
}
angular
.module('app')
.directive('allEmptyAllFull', allEmptyAllFull);
I've tried putting an ngModel.$validators.allEmptyAllFull function inside the link function, but I can't access the other two inputs while inside. I've seen other code where people put in a custom attribute inside the "comparison" form input but that hasn't gotten me anywhere in this case. Any help much appreciated.
EDIT: my initial question was worded poorly, sorry. I don't want any of the inputs to be independently required, but if someone enters text into any one of them, the other two need to be completed. All or none.