24

I was wondering if there is a way to validate a form in Angular 2 without using the form tag? For example below I want to make it a required field

<div class="form-group">
    <label for="name">Name</label>
    <input type="text" class="form-control" id="name">
</div>
<button type="button" class="btn btn-default" (click)="save()">Save</button>
4
  • 3
    May I ask why you don't want to use form tag? Commented Sep 13, 2016 at 5:44
  • I was just wondering if there is a way, I think for me it's better. I can find that it can be done in angularjs 1 but I cannot see any in angular 2 @rook Commented Sep 13, 2016 at 5:52
  • Angular detects form tag and adds functionality around it. This tag shouldn't affect any of your layout. Commented Sep 13, 2016 at 5:59
  • If you want field level validation, form is best option. Commented Sep 13, 2016 at 6:27

2 Answers 2

28

Form controls can be standalone (without a parent form), whether using declarative form controls or reactive form controls.

For declarative (using ngModel) you can just do something like

<input #model="ngModel" [(ngModel)]="value" type="text" required/>
<div *ngIf="model.invalid" style="color: red">Required</div>

For reactive forms you can do something like

<input [formControl]="control" [(ngModel)]="value" type="text"/>
<div *ngIf="control.invalid" style="color: red">Required</div>

// in component
this.control = new FormControl('', [Validators.required]);

See Plunker

For more information on using Angular forms in general, see the docs/tutorial

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

1 Comment

Had to change version of zone.js to 0.8.12. See this fork: plnkr.co/edit/o1TwYdWzb3NgXZrDWGz0?p=preview
13

Apart from using form controls directly as @peeskillet has shown in his answer, you can attach the form directive to any tag using its ngForm selector:

<div ngForm #newRuleForm="ngForm">
  <div class="form-group">
    <label for="name">Name</label>
    <input type="text" class="form-control" id="name">
  </div>
  <button type="button" class="btn btn-default" [disabled]="!newRuleForm.valid" (click)="save()">Save</button>
</div>

Keep in mind that you can't use standard form functionality like <button type=submit> in this case, but I find this approach very valuable when I want to use template-driven forms while retaining a simple way to validate a group of fields together.

4 Comments

Hey man, this is what I'm looking for. But how do you add validations this way? I tried using FormBuilder, but it didn't work.
@eestein since these are plain template based forms, attaching validations works the same as with any template based form: angular.io/guide/form-validation#simple-template-driven-forms
There is no directive with "exportAs" set to "ngForm" error when adding ngform to div
Button is not disabled: not working - <div class="visitor-entry" ngForm #newRuleForm="ngForm"> <!-- [ngClass]="{'validate':vName.errors && (vName.dirty || vName.touched)}"--> <div class="form-group"> <label for="name">Name</label> <input type="text" class="form-control" id="name"> </div> <div class="add-btn-cont"> <button [disabled]="!newRuleForm.valid" class="icon-btn" id="addVisitor" title="Add" (click)="addVisitor();"> <i class="fa fa-plus-circle"></i></button> </div> </div>

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.