1

I have created tab controls and each tab is using different controller. Each tab has some input types and also it has got validations. I have used form for each tabs and different controller for each tabs. I want to validate the form when i click on another tab. For example if there is any invalid value in Tab 1 then when user clicks on Tab 2 it should validate Tab 1 and if any invalid value is found then it should focus the invalid field and do not allow to switch tabs. Switching of tabs should be allowed only when form in the tab is valid.

Now i am not able to check whether form is valid or not during tab switch because tab DOM is out side the form and its controller. So i cant access formname.$valid property. So how i can handle this scenario?

Here is sample plunker

3 Answers 3

1

In plunker example, you have used different form for different template and included using ng-include. Instead you can make only one form and in individual template add only required form element instead of individual form. Use form validation on click event of specific tab and check whether input is valid or not. If input is not valid show error message and prevent switching from current tab to next tab.

Notes: Don't use ng-if for showing or hiding template instead use ng-show or ng-hide

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

5 Comments

If i use ng-show or ng-hide then all the tabs will be rendered in the initial load of page, but by using ng-if i can load tabs content on click of tab. In real scenario there exits large no. of controls in each tab. May i know why i should not use ng-if? Please advice. I have asked about same in this question stackoverflow.com/questions/33145644/…
Check my answer
Thats true . But in my case i am setting the expression to true once the tab is activated and it will not be set to false again. So i think there is no question of dom being removed.Correct me if i am wrong.
If i use ng-show or ng-hide then all the tabs will be rendered in the initial load of page, but by using ng-if i can load tabs content on click of tab. In real scenario there exits large no. of controls in each tab. Here DOM element is removed using ng-if is correct but it doesn't mean that it removed before it attatched to DOM. First HTML page is loaded and after variable bind to scope, ng-if removed DOM element so it's better to use ng-show because ng-if takes time to add and remove DOM element...
Ok. So what approach need to be used for my question here . stackoverflow.com/questions/33145644/…
0

while click on the second tab from second controller call controller of first tab and check form data is set or not.if not set call ng-click function of first tab. we can communicate between two controllers using following ways\ 1)sharing a data service

   function FirstController(someDataService) 
{
  // use the data service, bind to template...
  // or call methods on someDataService to send a request to server
}

function SecondController(someDataService) 
{
  // has a reference to the same instance of the service
  // so if the service updates state for example, this controller knows about it
}

2)emit event on the scope

      function FirstController($scope) 
{
  $scope.$on('someEvent', function(event, args) {});
  // another controller or even directive
}

function SecondController($scope) 
{
  $scope.$emit('someEvent', args);
}

Comments

0

You can do this:

Step1: Inject the $rootScope service in the controllers.

Step2: Now make a $rootScope variable say $rootScope.validForm=false;

Step3: Now you can set/check in every controller

   if(formname.$valid){
   //Your code
   $rootScope.validForm=true;
   }else{
   $rootScope.validForm=false;
   }

Step4: set ng-disable="$root.validForm===false" in the html

You can play with this rootScoep variable to maipulate accordingly. More over your structure is not correct make one form , also use ng-repeat for tab generation, need lot of improvements in your code.

Comments

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.