1

I'm currently trying to add validation to an angular 2 form, but for some reason I can't get my submit button to disable when the required fields are not filled in. Here is the code of my form template:

<h1 md-dialog-title>{{title}}</h1>
<form #formCtrl="ngForm">
    <md-dialog-content>
        <md-input-container>
            <input #name md-input placeholder="Name" value="" name="name" focused required>
        </md-input-container>
        <br />
        <md-select #inputtype placeholder="Input type" name="inputtype">
            <md-option *ngFor="let inputtype of inputtypes" [value]="inputtype.id">
            {{inputtype.name}}
            </md-option>
        </md-select>
    </md-dialog-content>
    <md-dialog-actions>
        <button type="submit" md-raised-button color="primary"  [disabled]="!formCtrl.form.valid">Create</button>
    </md-dialog-actions>
</form>

According to several examples, this should work, however the button is never disabled. I've also tried "!formCtrl.valid", also to no avail. I've tried using non-material design input fields thinking maybe that would be the issue, but it still won't work.

I then tried copy/pasting the following simple example into my application, and even that won't work at all: http://blog.angular-university.io/introduction-to-angular-2-forms-template-driven-vs-model-driven/

Any ideas as to what might prevent it from working correctly?

1
  • I may be wrong, but did you try without using the value attribute ? something like : <input #name md-input placeholder="Name" name="name" focused required> Commented Feb 2, 2017 at 16:21

1 Answer 1

3

Assuming you are using a newer release than Angular 2 final:

You need to add ngModel, which binds the form value based on the name attribute’s value. In your case one is name="inputtype" the other is name="name". So you need to add ngModel to bind the values, and your form should work as you wish! :)

So the following should work (removed a bit of noise from code):

<form #formCtrl="ngForm" (ngSubmit)="save(formCtrl.value)"> //whatever your submit method is
    <md-dialog-content>
        <md-input-container>
            <input md-input name="name" required ngModel>
        </md-input-container>
        <md-select name="inputtype" required ngModel>
            <md-option *ngFor="let inputtype of inputtypes" [value]="inputtype.id">
            {{inputtype.name}}
            </md-option>
        </md-select>
    </md-dialog-content>
    <md-dialog-actions>
        <button type="submit" md-raised-button color="primary"  [disabled]="!formCtrl.form.valid">Create</button>
    </md-dialog-actions>
</form>

Don't remember when this was introduced, this should be found somewhere in the changelogs which can be useful to have a look at once in a while, since Angular is constantly tweaking things almost in every release. So following that will keep you updated with changes and syntax :)

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

2 Comments

This solved the issue for me. Thanks for letting me know.
No problem, glad I could help! :)

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.