Reactive form in Angular Timepicker component

27 Sep 20234 minutes to read

TimePicker is a form component and validation is playing vital role in forms to get the valid data. Here to showcase the TimePicker with form validations we have used the reactive form.

  • The reactive forms uses the reactive model-driven technique to handle form data between component and view, due to that we also call it as the model-driven forms. It’s listen the form data changes between App component and view also returns the valid states and values of form elements.

For more details about Reactive Forms refer: https://angular.io/guide/reactive-forms.

  • For the reactive forms, import a ReactiveFormsModule into app module as well as the FormGroup,
    FormControl should be imported to app component. The FormGroup is used to declare formGroupName for the form and the FormControl is used to declare formControlName for form controls. You can declare the formControlName to TimePicker component as usual. Then, create a value object to the FormGroup and each value will be the default value of the form control.

The following example demonstrates how to use the reactive forms.

import { Component, ViewChild, OnInit, ElementRef, Inject } from '@angular/core';
import { TimePickerComponent } from '@syncfusion/ej2-angular-calendars';
import { FormValidator, FormValidatorModel } from '@syncfusion/ej2-inputs';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
import { ButtonComponent } from '@syncfusion/ej2-angular-buttons';
import { FormsModule } from '@angular/forms';

@Component({
    selector: 'app-root',
    templateUrl: './template.html'
})

export class AppComponent implements OnInit {
    @ViewChild('ejTimePicker') ejTimePicker?: TimePickerComponent;
    public targetElement?: HTMLElement;
    public placeholder: String = 'Select a time';
    skillForm?: FormGroup | any;
    build: FormBuilder;
    constructor(@Inject(FormBuilder) private builder: FormBuilder) {
        this.build = builder;
        this.createForm();
    }
    ngOnInit(): void {
        
    }
    createForm() {
        this.skillForm = this.build.group({
            timepicker: ['', Validators.required],
            username: ['', Validators.required],
            usermail: ['', Validators.email],
        });
    }
    get username() {
        return this.skillForm?.get('username');
    }
    get timepicker() {
        return this.skillForm?.get('timepicker');
    }
    get usermail() {
        return this.skillForm?.get('usermail');
    }

    onSubmit() {
        alert("Form Submitted!");
    }
}
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
import { AppComponent } from './app.component';
import { TimePickerModule } from '@syncfusion/ej2-angular-calendars';
import { ButtonModule } from '@syncfusion/ej2-angular-buttons';
/**
 * Module
 */
@NgModule({
    imports: [
        BrowserModule,FormsModule, ReactiveFormsModule, TimePickerModule, ButtonModule
    ],
    declarations: [AppComponent],
    bootstrap: [AppComponent]
})
export class AppModule { }
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { enableProdMode } from '@angular/core';
import { AppModule } from './app.module';

import 'zone.js';
enableProdMode();
platformBrowserDynamic().bootstrapModule(AppModule);