Validation in Angular Rich text editor component

27 Sep 202310 minutes to read

The Rich Text Editor supports both the reactive and template-driven form-building technologies.

Template driven forms

The template-drive forms use the angular 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.

Mention the name attribute to Rich Text Editor element that can be used to identify the form element. To register a Rich Text Editor element to ngForm, give the ngModel to it. So, the FormsModule will automatically detect the Rich Text Editor as a form element. After that, the Rich Text Editor value will be selected based on the ngModel value.

The following example demonstrates how to achieve a two-way data binding.

import { Component, ViewChild } from '@angular/core';
import { ToolbarService, LinkService, ImageService, HtmlEditorService, RichTextEditorComponent  } from '@syncfusion/ej2-angular-richtexteditor';
@Component({
    selector: 'app-root',
    template: `<div class="control-section">
    <div class="content-wrapper">
        <div id='content' class='box-form' style="margin: 0 auto; max-width:750px; padding:25px">
            <form (ngSubmit)="onSubmit()" #rteForm="ngForm">
                <div class="form-group">
                    <ejs-richtexteditor #fromRTE #name='ngModel' [(value)]='value' required name="name"
                        [(ngModel)]="value" (created)="rteCreated()"></ejs-richtexteditor>
                    <div *ngIf="(name.invalid && name.touched)" class="alert alert-danger">
                        <div *ngIf="name.errors!['required']">
                            Value is required.
                        </div>
                    </div>
                </div>
                <div>
                    <button type="submit" ejs-button [disabled]="!rteForm.valid">Submit</button>
                    <button type="reset" ejs-button style="margin-left: 20px">Reset</button>
                </div>
            </form>
        </div>
    </div>
</div>`,
    providers: [ToolbarService, LinkService, ImageService, HtmlEditorService]
})
export class AppComponent  {

    public value: string |null = null;

    @ViewChild('fromRTE')
    private rteEle: RichTextEditorComponent | undefined;

    rteCreated(): void {
        this.rteEle!.element.focus();
    }

    onSubmit(): void {
        alert('Form submitted successfully');
    }
}
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { RichTextEditorAllModule } from '@syncfusion/ej2-angular-richtexteditor';
import { ButtonModule } from '@syncfusion/ej2-angular-buttons';
import { AppComponent } from './app.component';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';

/**
 * Module
 */
@NgModule({
    imports: [
        BrowserModule,
        RichTextEditorAllModule,
        FormsModule,
		ButtonModule,
        ReactiveFormsModule
    ],
    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);

Reactive forms

The reactive forms use the reactive model-driven technique to handle form data between the component and view. So, it is called as model-driven forms. It listens 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 to: 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 group and the FormControl is used to declare formControlName for form controls. You can declare the formControlName to Rich Text Editor a value object must be created 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, OnInit, ViewChild } from '@angular/core';
import { ToolbarService, LinkService, ImageService, HtmlEditorService, RichTextEditorComponent } from '@syncfusion/ej2-angular-richtexteditor';
import { FormBuilder, FormControl, FormGroup, Validators } from '@angular/forms';
@Component({
    selector: 'app-root',
    template: `<div class="control-section">    <div class="content-wrapper">        <div id="content" class="box-form" style="margin: 0 auto; max-width:750px; padding:25px">            <form [formGroup]="rteForm" (ngSubmit)="onSubmit()">                <div class="form-group">                    <ejs-richtexteditor #fromRTE formControlName="name" (created)="rteCreated()">                    </ejs-richtexteditor>                    <div *ngIf="rteForm!.invalid && rteForm!.controls['name'].touched" class="alert alert-danger">                        <div *ngIf="rteForm.controls['name'].errors!['required']">                            Value is required.                        </div>                    </div>                </div>                <div class="form-group">                    <button type="submit" ejs-button [disabled]="!rteForm.valid">Submit</button>                    <button type="reset" ejs-button (click)="rteForm.reset()" style="margin-left: 20px">Reset</button>                </div>            </form>        </div>    </div></div>`,
    providers: [ToolbarService, LinkService, ImageService, HtmlEditorService]
})
export class AppComponent {

    rteForm!: FormGroup;

    @ViewChild('fromRTE')
    private rteEle: RichTextEditorComponent | undefined;

    constructor() {
        // <--- inject FormBuilder
    }

    ngOnInit(): void {
        this.rteForm = new FormGroup({
            'name': new FormControl(null, Validators.required)
        });
    }

    rteCreated(): void {
        this.rteEle!.element.focus();
    }

    onSubmit(): void {
        alert('Form submitted successfully');
    }
}
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { RichTextEditorAllModule } from '@syncfusion/ej2-angular-richtexteditor';
import { ButtonModule } from '@syncfusion/ej2-angular-buttons';
import { AppComponent } from './app.component';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';

/**
 * Module
 */
@NgModule({
    imports: [
        BrowserModule,
        RichTextEditorAllModule,
        FormsModule,
		ButtonModule,
        ReactiveFormsModule
    ],
    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);