Forms Support
You can use the Switch in Template-driven and 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.
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.
Managing the Switch Disabled State in Reactive Forms
To disable the Switch component when working with reactive forms and Angular 15 or later, use the FormControl's disabled property/option or disable() method.
Angular 15 introduces a breaking change affecting the synchronization of
setDisabledStatewith the model-DOM. This impacts components using thedisabledattribute. As a result, thedisabledproperty of the Kendo UI for Angular components used prior to Angular 15 is no longer working in reactive forms.
FormField Association
The Kendo UI for Angular FormField component enables you to group form-related elements, such as inputs, labels, hint and error messages, and configure their behavior.
The following example demonstrates how to use the Switch within a FormField with hint and error messages.
Mapping Boolean Values to Custom Form Values
The Switch uses boolean values in Angular forms. If your application stores a different value type, bind the Switch to a boolean FormControl and map the values when you load and save the form.
When you load the form, set the control to true if the stored value matches the value you use for the on state. Set it to false otherwise. When you save the form, map the control's boolean value to the corresponding stored value for the on or off state.
The following example starts with Y as the stored value. Toggle the Switch to see the boolean form control value and the corresponding Y or N value to store. Feel free to adapt the mapping logic to match the values used by your application.