hugo der hungrige
hugo der hungrige

Reputation: 12912

How to invalidate a form in angularjs? (or how to disable a form in angularjs?)

I want to preven form submission for a specific user type, preferably without having to edit something in the controller and without using an any type input-field? So basically something like this:

<form>
  <div class ="alert" 
       ng-if="user.type='not-allowed'" 
       maybe-an-invalidate-directive>
      <p>No! Don't!</p>

      <!-- or something like this ??? -->
      <input type ="hidden" 
             required
             ng-model="'false'">
  </div>
 .... 
</form>

While the first one should be definately possible, I would be happy to hear about a more straightforward solution.

EDIT:

After thinking about it for a while, I figured that what I want to do, is not making the form invalid, but to disable it. For a solution see this question.

Upvotes: 0

Views: 1907

Answers (1)

Brian Glaz
Brian Glaz

Reputation: 15666

You can use the ng-submit directive on your form element, and have it return false if you don't want it to submit.

<form ng-submit="user.type !== 'not-allowed' && controller.function()">

Upvotes: 1

Related Questions