0

I have a form input like so

<form name="groupForm" ng-submit="save()" ng-model="selectedItem"  novalidate>
<input 
    required 
    ng-disabled="true" 
    name="searchParameter" 
    type="text" 
    ng-model="criteria.SearchParameter" />

    <a href=""ng-click="save()" ng-class="{groupForm.$invalid}">Save All Changes</a>

When the value for the input is null or empty the form is invalid stating that this particular input is required. Is this expected or is there something I need to do?

5
  • 1
    You have required on the input? Therefore it is. Am I missing something? Commented Mar 26, 2015 at 13:53
  • But if it is required and disabled I would assume it is not then required? Commented Mar 26, 2015 at 13:53
  • 1
    No, required and disabled means it is required, but the user can't enter it. Commented Mar 26, 2015 at 13:54
  • I don't think they're exclusively linked tbh - I see what you mean but don't think it works like that Commented Mar 26, 2015 at 13:54
  • 1
    have a look at ng-required - you might be able to check if it's disabled which will let you decide whether it's required Commented Mar 26, 2015 at 13:55

2 Answers 2

2

You can use ng-required, and set a condition there:

<input 
     ng-required ="false"
     ng-disabled="true" 
     name="searchParameter" 
     type="text" 
     ng-model="criteria.SearchParameter" />

Check this fiddle.

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

Comments

0

You have the tag required in your input tag. That makes it required. In addition, you have ng-disabled="true" which means that you will never be able to fill in the input.

It seems like you have a misunderstand of what disabled does. Disabled just means you cant place input into it, not that it doesn't affect the form.

You are actually misusing disabled in 2 ways:

  1. Disabled and required together doesn't make sense (hence your problem)
  2. Specifically using the ng-disabled="true" also makes no sense. The advantage of ng-disabled is that it can be easily programaticly changed. Right now you are just hard-coding it. Might as well just use vanilla disabled

If you want the input to be required only when its enabled, and not required when its disabled, you can bind the two attributed to the same model:

<input 
    ng-required="foo"
    ng-disabled="foo" 
    name="searchParameter" 
    type="text" 
    ng-model="criteria.SearchParameter" />

4 Comments

How do I make it required only when it is enabled then?
I have a condition in there, but for the sakes of making the example simple, and removing doubt from the equation, I set it to false
This seems really counter-intuitive to me. Thanks though
@Chris You can use ng-required and bind it to the same model as ng-disabled. Ill update my answer with an example.

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.