Template driven forms in Angular Timepicker component

27 Sep 20234 minutes to read

The form can be build with Angular template syntax easily along with form specific directives. This template-driven forms uses the ng directives in view to handle the forms controls.

  • To enable the template-driven, import the FormsModule into corresponding app component.

For more details about template-driven Forms refer to:https://angular.io/guide/forms#template-driven-forms.

  • In angular forms mentioning the name is must to process as form elements.

  • Mention the name attribute to TimePicker element which will be used to identify the form element. To register an TimePicker element to ngForm, give the ngModel to it so the FormsModule will automatically detect the TimePicker as a form element. After that, the TimePicker value will be selected based on the ngModel value.

The following example demonstrates template driven forms with TimePicker component.

import { Component,ViewChild, ViewEncapsulation, Inject } from '@angular/core';
import { FormGroup, FormBuilder, Validators } from '@angular/forms';
import { TimePickerComponent } from '@syncfusion/ej2-angular-calendars';

class User {
    constructor() {
        
    }
}

@Component({
    selector: 'app-root',
    templateUrl: './template.html'
})
export class DefaultTimePickerComponent {
    public time?: Date
    user?: User | any;
    ngOnInit() {
        this.user = new User();
    }
    onSubmit(userForm:any) {
        (userForm.valid) ? alert("submitted"): alert("form is invalid");
    }
}
import { DefaultTimePickerComponent } from './app.component';
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
import { TimePickerModule } from '@syncfusion/ej2-angular-calendars';


@NgModule({
    imports: [ FormsModule, ReactiveFormsModule, BrowserModule, TimePickerModule],
declarations: [DefaultTimePickerComponent],
bootstrap: [DefaultTimePickerComponent]
})
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);
<div>
  <form #userForm="ngForm" (ngSubmit)="onSubmit(userForm)">
    <div class="form-group">
      <ejs-timepicker id="timepicker" name="time"  [(ngModel)]="user.time" #time="ngModel" width="300px" required></ejs-timepicker>
        <div  *ngIf="userForm.invalid && (userForm.dirty || userForm.touched)" class="alert alert-danger formerror">
          Valid time is required
        </div>
        <div *ngIf="userForm.valid && (userForm.dirty || userForm.touched)" class="alert alert-success formerror">
          <table>
            <tr>
              <td style="width:50%">Selected value: </td>
              <td class="formtext "></td>
            </tr>
          </table>
        </div>
    </div>
    <div class="buttons">
    <button type="submit" class="e-btn e-success">Submit</button>
    <button type="reset" class="e-btn e-warning">Reset</button>
    </div>
  </form>

  <div class="dataclass">userForm.value: </div>
  <div class="dataclass">userForm.valid: </div>

</div>

<style>
  form {
    margin-top: 50px;
  }
</style>