Search results

Use template-driven forms inside dialog

The following sample demonstrates how to use the template-driven forms with required validation inside the dialog. For more details, refer to the Angular Documentation

Source
Preview
app.component.ts
app.module.ts
main.ts
index.css
import { Component, OnInit, ViewChild, ViewEncapsulation } from '@angular/core';
import { UploaderComponent } from '@syncfusion/ej2-angular-inputs';
import { DialogComponent } from '@syncfusion/ej2-angular-popups';
import { isNullOrUndefined, EmitType } from '@syncfusion/ej2-base';

@Component({
  selector: 'app-root',
  template: `<div class="control-section">
        <div class="col-lg-12">
        <div class="control_wrapper" id="control_wrapper" style="margin: 10px auto;">
         <ejs-dialog id="FormDialog" #FormDialog width= '450px'
          [showCloseIcon]='false' target='.control-section'>
        <ng-template #content>
            <form id="template_driven" #userForm="ngForm" novalidate>
                <div class="form-group" style="padding-top: 11px;">
                    <div class="e-float-input">
                        <input type="text" id="name" #nameval='ngModel' name="name" required ngModel>
                        <span class="e-float-line"></span>
                        <label class="e-float-text e-label-top" for="name">Name</label>
                        <div *ngIf="(nameval.invalid && (nameval.dirty || nameval.touched))">
                            <div class="e-error" *ngIf="nameval.errors.required">
                                * Enter your name
                            </div>
                        </div>
                    </div>
                </div>

                <div class="form-group" style="padding-top: 11px;">
                    <div class="e-float-input">
                        <input type="text" id="email" #emailval='ngModel' name="email" required ngModel>
                        <span class="e-float-line"></span>
                        <label class="e-float-text e-label-top" for="email">Email</label>
                        <div *ngIf="(emailval.invalid && (emailval.dirty || emailval.touched))">
                            <div class="e-error" *ngIf="emailval.errors.required">
                                * Enter your email address
                            </div>
                        </div>
                    </div>
                </div>

                <div class="form-group" style="padding-top: 11px;">
                    <div class="e-float-input">
                        <input type="number" id="contact" #contactval='ngModel' name="contact" required ngModel>
                        <span class="e-float-line"></span>
                        <label class="e-float-text e-label-top" for="contact">Contact No</label>
                        <div *ngIf="(contactval.invalid && (contactval.dirty || contactval.touched))">
                            <div class="e-error" *ngIf="contactval.errors.required">
                                * Enter your contact number
                            </div>
                        </div>
                    </div>
                </div>

                <div class="selected-files">
                  <div class="textboxes">
                    <div class="form-group" style="padding-top: 11px; width:">
                          <div class="e-float-input upload-area">
                              <input type="text" id="upload" #uploadval='ngModel' [(ngModel)]="uploadInput" readonly name="upload" required ngModel>
                              <span class="e-float-line"></span>
                              <label class="e-float-text e-label-top" for="upload">Choose a file</label>
                          </div>
                      </div>
                  </div>
                  <div class="uploader-section">
                      <button id="browse" class="e-control e-btn e-info" (click)='browseClick()'>Browse...</button>
                      <ejs-uploader #defaultupload id='fileupload' allowedExtensions="image/*" [autoUpload]=false [multiple]='multiple' (selected)='onFileSelect($event)'></ejs-uploader>
                  </div>
                </div>
                <span class='error-root' *ngIf="(uploadval.invalid && (uploadval.dirty || uploadval.touched))">
                    <span class="e-error errorClass" *ngIf="uploadval.errors.required">
                        * Select a file
                    </span>
                </span>
                <div class="form-group" style="padding-top: 11px;">
                  <div class="submitBtn">
                      <button class="submit-btn e-btn" id="submit-btn" [disabled]="userForm.invalid" type="reset" (click)= "Submit()">Submit</button>
                      <div class="desc"><span>*This button is not a submit type and the form submit handled from externally.</span></div>
                  </div>
                </div>
          </form>
          </ng-template>
          </ejs-dialog>

    </div>
  </div>
</div>
         <ejs-dialog id="confirmationDialog" #Dialog [buttons]='dlgButtons' [animationSettings]='animationSettings' [header]='formHeader' [showCloseIcon]='showCloseIcon' [content]='contentData'  [target]='target' [width]='width' [visible]="visible" [isModal]="isModal" >
          </ejs-dialog>`
})
export class AppComponent  {
  @ViewChild('formUpload')
  public uploadObj: UploaderComponent;
  @ViewChild('Dialog')
  public dialogObj: DialogComponent;
  public width: string = '335px';
  public visible: boolean = false;
  public multiple: boolean = false;
  public showCloseIcon: Boolean = true;
  public formHeader: string = 'Success';
  public contentData: string = 'Your details have been updated successfully, Thank you.';
  public target: string = '#FormDialog';
  public isModal: boolean = true;
  public animationSettings: object = {
    effect: 'Zoom'
  };
  public uploadInput: string = '';
  public dlgBtnClick: EmitType<object> = () => {
    this.dialogObj.hide();
  }
  public dlgButtons: Object[] = [{ click: this.dlgBtnClick.bind(this), buttonModel: { content: 'Ok', isPrimary: true } }];
    @ViewChild('formElement') element: any;

  public browseClick() {
     document.getElementsByClassName('e-file-select-wrap')[0].querySelector('button').click(); return false;
   }
   public Submit(): void {
    this.onFormSubmit();
  }
 public onFileSelect: EmitType<Object> = (args: any) => {
  this.uploadInput = args.filesData[0].name;
 }

 public onFormSubmit(): void {
   this.dialogObj.show();
 }
}
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';

import { AppComponent } from './app.component';
import { ButtonModule } from '@syncfusion/ej2-angular-buttons';
import { DialogModule } from '@syncfusion/ej2-angular-popups';
import { UploaderModule } from '@syncfusion/ej2-angular-inputs';


@NgModule({
  imports:      [ BrowserModule, FormsModule, DialogModule, UploaderModule, ButtonModule, ReactiveFormsModule ],
  declarations: [ AppComponent ],
  bootstrap:    [ AppComponent ]
})
export class AppModule { }
import { enableProdMode } from '@angular/core';
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';

import { AppModule } from './app.module';

platformBrowserDynamic().bootstrapModule(AppModule).then(ref => {
  // Ensure Angular destroys itself on hot reloads.
  if (window['ngRef']) {
    window['ngRef'].destroy();
  }
  window['ngRef'] = ref;

  // Otherwise, log the boot error
}).catch(err => console.error(err));
#container {
  visibility: hidden;
}

#loader {
  color: #008cff;
  height: 40px;
  left: 45%;
  position: absolute;
  top: 45%;
  width: 30%;
}.control_wrapper {
  max-width: 500px;
  min-width: 245px;
  margin: auto;
}
.address-field {
resize: none;
}

.uploader-section {
  position: relative;
  float: right;
  height: 50px;
  top: -46px;
}

.error-root {
  display: inline-block;
  position: relative;
  top: -9px;
}
.e-error {
  padding-top: 7px;
}
.textboxes {
  width: 67%;
}

.control-section {
  width: 500px;
  height: 600px;
  position: relative;
  margin: auto;
}

#control_wrapper {
max-width: 500px;
margin: auto;
}
.highcontrast #control_wrapper {
background: #000000;
}
#template_driven .upload-area {
width: 100%;
}

#template_driven .form-group .submit-btn {
margin-top: 15px;
position: relative;
position: relative;
}
#template_driven .form-group .desc {
margin: 2% 14% 0% 18%;
}
@media only screen and (max-width: 500px) {
  #template_driven .form-group .submitBtn .desc {
  margin: 12px;
}
}
#template_driven .form-group .submitBtn {
position: relative;
text-align: center;
float: right;
}
.bootstrap #template_driven .form-group .submit-btn {
margin-left: 8px;          
}
.e-bigger.material #template_driven .form-group .submit-btn {
margin-left: -7px;          
}
.fabric #template_driven .form-group .submit-btn, .highcontrast #template_driven .form-group .submit-btn {
margin-left: -11px;          
}
.e-bigger.fabric #template_driven .form-group .submit-btn, .e-bigger.highcontrast #template_driven .form-group .submit-btn {
margin-left: -25px;          
}
.e-bigger.bootstrap #template_driven .form-group .submit-btn {
margin-left: -8px;          
}
.success .successmsg {
border: 0.5px solid green;
padding: 10%;
color: green;
}
form#template_driven {
position: relative;
top: 14%;
}
#template_driven .e-upload{
display: none;
}
#template_driven .upload-area button#browse {
float: right;
margin-top: -28px;
}
.fabric #template_driven .upload-area button#browse,
.highcontrast #template_driven .upload-area button#browse {
  float: right;
  margin-top: -32px;
  }
  .bootstrap #template_driven .upload-area button#browse {
  float: right;
  margin-top: -35px;
  }
  .fabric.e-bigger #template_driven .upload-area button#browse,
.highcontrast.e-bigger #template_driven .upload-area button#browse,
.bootstrap.e-bigger #template_driven .upload-area button#browse {
    margin-top: -40px;
  }
  .material.e-bigger #template_driven .upload-area button#browse {  
    margin-top: -36px;
    }
  .b
#template_driven .form-group .e-float-input.upload-area .e-float-line, .material .e-float-input.upload-area .e-float-line {  
  width: 70%;
  margin-top: initial;
}
#template_driven .form-group .upload-area input {
width: 100%;
}

@media (min-width: 250px) and (max-width: 500px) {
#control_wrapper.control_wrapper {
  padding: 1% 4% 12%;
}
}
@media (max-width: 300px) {
#control_wrapper.control_wrapper {
  padding: 1% 4% 15%;
}
}
.bootstrap #template_driven .form-group .upload-area {
 background: transparent;
}
.form-title {
text-align: center;
}