1
$scope.forms = [
        {id: 1, text: 'h_form'},
        {id: 2, text: 'S_form'},
        {id: 3, text: 'l_form'},
        {id: 4, text: 'g_form'},
        {id: 5, text: 'f_form'},
        {id: 7, text: 'b_form'},
        {id: 8, text: 's_form'}
      ];

I'm achieving some validation with these codes but thats weired called multiple times that's not correct

if($scope.form.userInfo.h_form != undefined){
        if($scope.form.userInfo.h_form.$invalid){
         if(setPlayerProfileId == ''){
          setPlayerProfileId = 'sportdetailform';
         }
        }
      }

      if($scope.form.userInfo.g_form != undefined){
        if($scope.form.userInfo.g_form.$invalid){
         if(setPlayerProfileId == ''){
          setPlayerProfileId = 'sportdetailform';
         }
        }
      }

      if($scope.form.userInfo.f_form != undefined){
        if($scope.form.userInfo.f_form.$invalid){
         if(setPlayerProfileId == ''){
          setPlayerProfileId = 'sportdetailform';
         }
        }
      }

I typically do not want to run this code multiple time so i used foreach function but doesn't work for me please suggest how to fixed out those

angular.forEach($scope.forms, function(value, key){
          var check = value.text;
            if($scope.form.userInfo.check != undefined){
              if($scope.form.userInfo.check.$invalid){
               if(setPlayerProfileId == ''){
                setPlayerProfileId = 'sportdetailform';
               }
              }
            }

      })

1 Answer 1

1

Change your forEach in the following way:

angular.forEach($scope.forms, function(value, key){
        if($scope.form.userInfo[value.text] != undefined){
          if($scope.form.userInfo[value.text].$invalid){
           if(setPlayerProfileId == ''){
            setPlayerProfileId = 'sportdetailform';
           }
          }
        }

  });
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks Vivz for this.
Glad to be of help:)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.