AngularJS Form Validation inside an ng-repeat -


so trying validate input of 1 item inside of ng-repeat. examples sake lets have 5 items (1,2,3,4,5) , want validate form if 4th item selected.

i have used ng-pattern before validate forms, not 1 had dropdown menu select item.name

i have included regex 4th item validated inside ng-pattern.

<div>   <select name="name" ng-model="item.name" ng-options="item item in items" required></select> </div>  <div>   <input name="results" type="text" ng-model="item.results" ng-pattern="/^\d\d\d\/\d\d\d/" required> </div> 

any suggestions correct way validate situation appreciated. have thought creating directive validate this, feels overly complicated solution since not use directive more once in app.

//////////////////////////////////////////////////

it wouldn't let me answer own question here answer figured out.

what ended having use ng-pattern , pass function.

<input name="results" type="text" ng-model="vital.results" ng-pattern="vitalregex()" required> 

here controller code

$scope.item4regex = /^\d{2,3}\/\d{2,3}$/;    $scope.itemregex = function() {     if($scope.item && $scope.item.name === "fourth item")       return $scope.item4regex;     else return (/^$/);   }; 

or else...

add ng-change directive on select dropdown calls controller method , controller method sets flag whether validate form or not.

eg.

<select ng-change="checkifformshouldbevalidated()" ng-model="item.name"></select>  // inside controller $scope.checkiffromshouldbevalidated = function(){    if( $scope.item.name == 4th item ) $scope.shouldvalidate = true;    else $scope.shouldvalidate = false; };   $scope.formsubmit = function(){      if(($scope.shouldvalidate && form.$valid) || (!$scope.shouldvalidate)){         // submit form       }  }; 

see if helps.


Comments