Forms Support

You can use the Switch in template-driven or reactive forms.

Template-Driven Forms

The template-driven forms enable you to bind the value of the Switch to the forms model by using the ngModel directive.

The following example demonstrates how to use the Switch in a template-driven form.

@Component({
    selector: 'my-app',
    template: `
        <form #form="ngForm">
            <div class="example-config">
                <p>The switch value is: <strong>{{ value }}</strong></p>
            </div>
            <label>
              Select value:
                 <kendo-switch
                  name="switch"
                  [(ngModel)]="value"
                >
                </kendo-switch> <br />
              </label>
        </form>
    `
   })
export class AppComponent {
    public value: number = true;
}

Reactive Forms

The FormGroup provides a way to render reactive forms. For more details, refer to the Angular Documentation.

The following example demonstrates how to use the Switch in a reactive form.

import {
       ReactiveFormsModule,
       FormGroup,
       FormControl,
       Validators
   } from '@angular/forms';

   @Component({
       selector: 'my-app',
       template: `
       <form [formGroup]="form">
           <div class="example-config">
               <p>The form is valid: <strong>{{ form.controls.switch.valid }}</strong></p>
               <p>The form switch value is: <strong>{{ form.controls.switch.value }}</strong></p>
           </div>
           <kendo-switch formControlName="switch"></kendo-switch>
       </form>
       `
   })
   class AppComponent {
       public form = new FormGroup({
           switch: new FormControl(null, Validators.required)
       })
   }

FormField Association

The following example demonstrates how to use the Switch within a FormField component and provide hint and error messages.


import { Component, ViewEncapsulation } from '@angular/core';
import { Validators, FormGroup, FormControl } from '@angular/forms';

@Component({
    selector: 'my-app',
    template: `
        <div class="example">
            <form class="k-form" [formGroup]="form">
                <kendo-formfield>
                    <kendo-label [for]="allow" text="Allow Notifications"></kendo-label>
                    <kendo-switch #allow [formControlName]="'notifications'"></kendo-switch>
                    <kendo-formhint>You will receive notifications</kendo-formhint>
                    <kendo-formerror>This field is required</kendo-formerror>
                </kendo-formfield>
            </form>
        </div>
    `,
    styles: [`
        .example {
            display: flex;
            justify-content: center;
        }
        .k-form { width: 400px; }
    `],
    encapsulation: ViewEncapsulation.None
})

export class AppComponent {
    public form: FormGroup;

    public userData: any = {
        notifications: true
    };

    constructor() {
        this.form = new FormGroup({
            notifications: new FormControl(this.userData.notifications, [Validators.requiredTrue])
        });
    }
}
import { CommonModule } from '@angular/common';
import { NgModule } from '@angular/core';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
import { BrowserModule } from '@angular/platform-browser';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';

import { LabelModule } from '@progress/kendo-angular-label';
import { InputsModule } from '@progress/kendo-angular-inputs';
import { AppComponent } from './app.component';

@NgModule({
    bootstrap:    [AppComponent],
    declarations: [AppComponent],
    imports:      [
        BrowserModule,
        BrowserAnimationsModule,
        CommonModule,
        FormsModule,
        ReactiveFormsModule,
        InputsModule,
        LabelModule
    ]
})
export class AppModule { }
import { enableProdMode } from '@angular/core';
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { AppModule } from './app.module';

enableProdMode();

const platform = platformBrowserDynamic();
platform.bootstrapModule(AppModule);

In this article