Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9230657
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T05:52:44+00:00 2026-06-18T05:52:44+00:00

I use ctrl.$setValidity within one of my directives to invalidate a form. However, there

  • 0

I use ctrl.$setValidity within one of my directives to invalidate a form. However, there is a method residing elsewhere which could remove this element from the DOM under a different set of conditions. What happens when I invalidate the form with $setValidity and then remove the offending element is that the form remains invalid, while what I would like it to do is to recalculate its validity based on its new Inputfield set.

Please note that I am not simply looking for a ctrl.$setValidity true, (other input fields in the form may or may not be valid), I simply want the form to recalculate.

Below is the directive code:

 app.directive('dir', function() {
   return {
      restrict: 'C',
      require: "ngModel",
      link: function(scope, element, attrs, ctrl) {
      someValue = scope.someValue;
      someField = element.find(".some-class");

      monitorField = function(newValue, oldValue){
        console.log("whee");
        scope.someFunction(newValue);
        if(newValue =="Invalidate NOW!"){
        ctrl.$setValidity('someClass', false);      
        } else if (oldValue == "Invalidate NOW!"){
           angular.element(document.querySelector('.some-class')).remove();
        } else {
          ctrl.$setValidity('someClass', true); 
       }
    }  
    scope.$watch("someValue", monitorField, true);
   }
 }
});

Which operates on:

   <FORM class="dir" ng-model="someValue">
     <INPUT type="text" class="some-class" ng-model="someValue"/>
     <INPUT type="text" class="some-other-class"/>
   </FORM>

(Yes it is a somewhat contrived example).

The problem is reproduced here: http://jsfiddle.net/kTuAY/

In my actual code, I populate the input fields based on an array of objects, which gets populated via Service, and remove elements via array.splice. The example given in the jsfiddle is merely there for simplicity.

Another interesting condition of failure can be seen in this fiddle:

http://jsfiddle.net/JACAv/

Specifically, if one of the inputs depends on non collision of value with the other, and is thusly invalidated, whereafter the other fields value is changed, validity remains incorrect.

Temporary not quite functioning fiddle while I go to work:

http://jsfiddle.net/yQpHL/

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T05:52:45+00:00Added an answer on June 18, 2026 at 5:52 am

    The way I ended up solving this was:

    1. Put a separate watch expression on the array being ng-repeated over.
    2. Within that watch expression, construct a hash of all the values currently present in input fields that match the relevant directive.
    3. Reset the value of those fields forcefully with .val(), and then call $compile on the inner fields to retrigger the inner watch expression
    4. Within the function adding the input field values on the controller (the function that gets called just before ctrl.$setValidity from the inner directive:
      • Verify that he number of times the value is entered is 1
      • Reset the array being ng-repeated over to contain the values of the hash

    The good:

    • When an element is removed,the outer watch (the one on the entire array) triggers, the hash is recomputed, the values of each field are reset and revalidated
    • When a collision is detected, it will bubble correctly, marking all colliding fields as invalid, when the collision is removed, all fields are re-validated

    The bad:

    • Calling $compile on every field every time the array changes is computationally inefficient
    • As the inner watch expression will trigger once when the value changes, and once when the outer watch expression calls compile, effectively the number of $digest‘s is doubled

    I’ll put together a code example just as soon as I sanitize the code.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm attempting to use the Ctrl + R , Ctrl + R command within
I use the following function to show controls on my form: class procedure TFormMain.FadeControls(ctrl:Array
There is a link with title and some value: <a href=http://site.com/someid/ title=Use ctrl +
I have some commands that use Ctrl-Win- as hotkeys. However, MenuItem displays these as
In TextMate , one can use ctrl-shift-w to wrap text in an Open/Close tag
I Want to use Ctrl + Space for omni-completion (and keyword completion if there
In vim, I use Ctrl-n to word complete while in insert mode. If there
In the previous versions of Visual Studio, I could use CTRL + -> and
I use CTRL + L all the time to delete rows in visual studio,
I often use Ctrl + P to open files, but then I'd like to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.