I want to a validation on a form in the angular way. I have three input boxes. All three are required when one of them is entered. Otherwise they are not required. Continue button must be enabled only when all three are entered or not enetered.
You can review the code on this plunker. Here's a snippet, too, just in case plunker is inconvenient:
angular.module('test', []).config(function() {});
angular.module('test').controller('testctrl', function($rootScope, $scope, $location) {
$scope.admin = [{
"number": "2"
}];
$scope.adminInfos = {};
$scope.validations = {};
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<form name="test_form">
<input type="text" name="admin{{admin.number}}_firstname" ng-model="adminInfos[admin.number].firstname" placeholder="First Name" ng-required="adminInfos[admin.number].lastname.length && adminInfos[admin.number].email.length" />
<span ng-show="!system_admins.admin{{admin.number}}_firstname.$pristine && (adminInfos[admin.number].email.length || adminInfos[admin.number].lastname.length) && !adminInfos[admin.number].firstname.length" class="error">First name is required</span>
<br/>
<input type="text" name="admin{{admin.number}}_lastname" ng-model="adminInfos[admin.number].lastname" placeholder="Last Name" ng-required="adminInfos[admin.number].firstname.length && adminInfos[admin.number].email.length" />
<span ng-show="!system_admins.admin{{admin.number}}_lastname.$pristine && (adminInfos[admin.number].email.length || adminInfos[admin.number].firstname.length ) && !adminInfos[admin.number].lastname.length" class="error">Last name is required</span>
<br/>
<input type="text" name="admin{{admin.number}}_email" ng-model="adminInfos[admin.number].email" placeholder="Email Address" ng-required="adminInfos[admin.number].firstname.length && adminInfos[admin.number].lastname.length" />
<span ng-show="!system_admins.admin{{admin.number}}_email.$pristine && !adminInfos[admin.number].email.length && (adminInfos[admin.number].lastname.length || adminInfos[admin.number].firstname.length)" class="error">Email is required</span>
<br/>
<br/>
<button ng-disabled="test_form.$invalid">Continue</button>
</form>
I have an issue where continue button is still enabled if I enter the first name and leave the last name and email as blank.
Continue button must be enabled if all three are not entered or all three are entered.