1

I have a form. In which I have input fields and I am doing some validation based on input. Also, there is a submit button which enables only if form is valid.

In this form I am enabling/disabling input field in one of the flow. Initially, when the fields are enable and empty, create button is disabled. After i disable and enable the fields, create button becomes enabled. Although input fields are still empty. More over button which is enabling/disabling this part is outside of this form.

Here is my code

`

<form method="post" novalidate id="example-widgets-form" name="mdnsCtrl.createSubDomainForm" valdr-type="SubDomain">
<div>
    <label>Domain Name</label>
    <input required type="text" name="subDomainName" placeholder="Domain Name" ng-model="mdnsCtrl.newDomain.name">
</div>
<div>
    <label>Description</label>
    <input type="text" name="subDomainDescription" placeholder="Description (Optional)" ng-model="mdnsCtrl.newDomain.description">
</div>
    <button type="button" aria-label="Create" ng-click="mdnsCtrl.createDomain();" 
    ng-disabled="mdnsCtrl.createSubDomainForm.$invalid">
        <span class="ng-scope">Create</span>
    </button>
</div>
</form>

Tried few things like using $setUntouched() and $setPristine(). But nothing is working. Any help will be appreciated.

Adding a codepen example for this: code

Much Thanks. `

2

2 Answers 2

2

Its not good practice to mix Angular with jQuery. Please read this great post: “Thinking in AngularJS” if I have a jQuery background?

You can easily achieve requested behavior by using ng-disabled="mdnsCtrl.formDisabled"

JS

var ctrl = this;  
  ctrl.formDisabled = false;

  this.disable = function(){
    ctrl.formDisabled = true;  
  };

  this.enable = function(){
    ctrl.formDisabled = false;
  };

HTML

<div>
    <label>Domain Name</label>
    <input class="input-field" required type="text" name="subDomainName" placeholder="Domain Name"
           ng-disabled="mdnsCtrl.formDisabled"
           ng-model="mdnsCtrl.name" >
</div>
<div>
    <label>Description</label>
    <input class="input-field" type="text" name="subDomainDescription"
            ng-disabled="mdnsCtrl.formDisabled"
           placeholder="Description (Optional)" ng-model="mdnsCtrl.description">
</div>

Fixed Demo Codepen

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

2 Comments

It did indeed ! Thanks a lot for this solution. Although I found another solution to my problem. Instead of using $(".create-panel *").prop('disabled', true); I used $("input[type='text']").prop('disabled', true); I went through the SO link, it really gave me insight on this.
@Maxim Shoustin. But I still have one question. Why was my previous way failing ? Not understanding that part. Why parent div selection made whole form valid ? Please provide insight on this too.
0

I think you missed required attribute for Description input..

<input type="text" name="subDomainDescription" required ng-model="mdnsCtrl.newDomain.description">

1 Comment

Description field is optional.

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.