Forms Support

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

The component accepts values of the complex (objects) or the primitive (strings or numbers) type. To specify the value type, set the valuePrimitive property. By default, it is set to false.

Template-Driven Forms

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

The following example demonstrates how to accomplish a two-way data binding by using the ngModel directive. The model field is a complex value.

@Component({
  selector: 'my-app',
  template: `
    <form #form="ngForm">
      <div class="example-config">
        The selected values are <strong>{{ gender | json }}</strong>
      </div>
      <div class="example-wrapper">
        <p>Select Gender:</p>
        <kendo-multiselect
            name="gender"
            [data]="genders"
            [textField]="'text'"
            [valueField]="'value'"
            [(ngModel)]="gender"
        >
        </kendo-multiselect>
      </div>  
    </form>
  `
})
class AppComponent {
    public genders: Array<{ text: string, value: number }> = [
        { text: "Male", value: 1 },
        { text: "Female", value: 2 },
        { text: "Other", value: 3 }
    ];

    public gender = [{ text: "Female", value: 2 }];
}

The following example demonstrates how to accomplish a two-way data binding by using the ngModel directive. The model field is a primitive value.

@Component({
  selector: 'my-app',
  template: `
    <form #form="ngForm">
      <div class="example-config">
        The selected values are <strong>{{ gender | json }}</strong>
      </div>
      <p>Select Gender:</p>
      <kendo-multiselect
          name="gender"
          [data]="genders"
          [textField]="'text'"
          [valueField]="'value'"
          [valuePrimitive]="true"
          [(ngModel)]="gender"
      >
      </kendo-multiselect>
    </form>
  `
})
class AppComponent {
    public genders: Array<{ text: string, value: number }> = [
        { text: "Male", value: 1 },
        { text: "Female", value: 2 },
        { text: "Other", value: 3 }
    ];

    public gender = [2];
}

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 MultiSelect in a reactive form with a primitive value binding.
 

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

@Component({
  selector: 'my-app',
  template: `
    <form [formGroup]="myForm">
      <div class="example-config">
        The form.gender value is: <strong>{{ myForm.controls.gender.value | json }}</strong>
      </div>
      <p>Select Gender:</p>
      <kendo-multiselect
          formControlName="gender"
          [data]="genders"
          [textField]="'text'"
          [valueField]="'value'"
          [valuePrimitive]="true"
      >
      </kendo-multiselect>
    </form>
  `
})
class AppComponent {
    public genders: Array<{ text: string, value: number }> = [
        { text: "Male", value: 1 },
        { text: "Female", value: 2 },
        { text: "Other", value: 3 }
    ];

    public myForm: FormGroup = new FormGroup({
        gender: new FormControl([2])
    });
}

The following example demonstrates how to use the MultiSelect in a reactive form with a complex value binding.
 

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

@Component({
  selector: 'my-app',
  template: `
    <form [formGroup]="myForm">
      <div class="example-config">
        The form.gender value is: <strong>{{ myForm.controls.gender.value | json }}</strong>
      </div>
      <p>Select gender</p>
      <kendo-multiselect
          formControlName="gender"
          [data]="genders"
          [textField]="'text'"
          [valueField]="'value'"
      >
      </kendo-multiselect>
    </form>
  `
})
class AppComponent {
    public genders: Array<{ text: string, value: number }> = [
        { text: "Male", value: 1 },
        { text: "Female", value: 2 },
        { text: "Other", value: 3 }
    ];

    public myForm: FormGroup = new FormGroup({
        gender: new FormControl([{ text: "Female", value: 2 }])
    });
}

FormField Association

The following example demonstrates the usage of the MultiSelect within a FormField with hint and error messages.

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

@Component({
    selector: 'my-app',
    template: `
        <div class="example">
            <form class="k-form" [formGroup]="form">
                <kendo-formfield>
                    <kendo-label [for]="sport" text="Preferred sport"></kendo-label>
                    <kendo-multiselect
                        #sport
                        [formControlName]="'sport'"
                        [data]="sports"
                        (valueChange)="handleValueChange($event)"
                    >
                    </kendo-multiselect>
                    <kendo-formhint>You can pick up to three sports</kendo-formhint>
                    <kendo-formerror>Required</kendo-formerror>
                </kendo-formfield>
            </form>
        </div>
    `,
    styles: [`
        .example {
            display: flex;
            justify-content: center;
        }
        .k-form {
            width: 400px;
        }
    `]
})
export class AppComponent {
    public form: FormGroup = new FormGroup({
        sport: new FormControl(null, Validators.required)
    });

    public sports: string[] = ['Football', 'Baseball', 'Basketball', 'Cricket', 'Field Hockey', 'Table Tennis', 'Tennis', 'Volleyball'];

    public handleValueChange(value: string[]): void {
        if (value && value.length > 3) {
            const slicedItems = value.slice(0, 3);
            this.form.controls.sport.setValue(slicedItems);
        }
    }
}
import { NgModule } from '@angular/core';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
import { BrowserModule } from '@angular/platform-browser';
import { DropDownsModule } from '@progress/kendo-angular-dropdowns';
import { LabelModule } from '@progress/kendo-angular-label';
import { InputsModule } from '@progress/kendo-angular-inputs';
import { AppComponent } from './app.component';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';

@NgModule({
    declarations: [AppComponent],
    imports: [
        BrowserModule,
        BrowserAnimationsModule,
        FormsModule,
        ReactiveFormsModule,
        DropDownsModule,
        LabelModule,
        InputsModule
    ],
    bootstrap: [AppComponent]
})

export class AppModule { }
import { AppModule } from './app.module';
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';

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

In this article