Start by installing the rxweb package from the npm using the below command :<\/p>\n
\nnpm i @rxweb\/reactive-form-validators<\/p>\n<\/blockquote>\n
Register the RxReactiveFormsModule<\/code> module as below in the root(App) module:<\/p>\n@NgModule({ declarations<\/span>: [AppComponent,UserComponent], entryComponents<\/span>: [], imports<\/span>: [BrowserModule,RxReactiveFormsModule, CommonModule,FormsModule, ReactiveFormsModule, IonicModule.forRoot(), AppRoutingModule], providers<\/span>: [ StatusBar, SplashScreen, { provide<\/span>: RouteReuseStrategy, useClass<\/span>: IonicRouteStrategy } ], bootstrap<\/span>: [AppComponent]\n})\nexport<\/span> class<\/span> AppModule<\/span> <\/span>{}\n<\/code><\/pre>\nLets begin the implementation by creating a model class<\/p>\n
This model contains a class Named User with the required properties in which the annotation is used from the rxweb validators, in this the model can contain one or more annotation<\/p>\n
import<\/span> { required } from<\/span> '@rxweb\/reactive-form-validators'<\/span>; export<\/span> class<\/span> User<\/span> <\/span>{ @required() firstName<\/span>: string; @required() lastName<\/span>: string; @required() Email<\/span>: string;\n}\n<\/code><\/pre>\nUsing the above model the FormGroup is bind using the formGroup<\/code> method of the RxFormBuilder<\/code> which is responsible for the forming of the userFormGroup as mentioned below in the code. The FormGroup which consist of the properties of the user model as a FormControl along with the applied validations.<\/p>\nimport<\/span> { Component, OnInit } from<\/span> '@angular\/core'<\/span>;\nimport<\/span> { FormGroup } from<\/span> '@angular\/forms'<\/span>;\nimport<\/span> { RxFormBuilder } from<\/span> '@rxweb\/reactive-form-validators'<\/span>;\nimport<\/span> { User } from<\/span> '.\/user.model'<\/span>; @Component({ selector<\/span>: 'app-user'<\/span>, templateUrl<\/span>: '.\/user.component.html'<\/span>, styleUrls<\/span>: ['.\/user.component.scss'<\/span>],\n})\nexport<\/span> class<\/span> UserComponent<\/span> implements<\/span> OnInit<\/span> <\/span>{ userFormGroup<\/span> : FormGroup constructor<\/span>(private formBuilder: RxFormBuilder) { } ngOnInit() { this<\/span>.userFormGroup = this<\/span>.formBuilder.formGroup(User); } userRegistrationFormGroupSubmit() { console<\/span>.log(this<\/span>.userFormGroup.getRawValue()); }\n} <\/code><\/pre>\nAs we are using the ionic framework which is used for hybrid cross platform application development in which we will use the ion tags for making it compatible for mobile and web both.<\/p>\n
\nHere the validation messages are displayed from the globally configured in the app component during the initialization of the application as below<\/p>\n<\/blockquote>\n
initializeApp() { ReactiveFormConfig.set({\"validationMessage\"<\/span>:{\"required\"<\/span>:\"This field is required.\"<\/span>}}); this<\/span>.platform.ready().then(()<\/span> =><\/span> { this<\/span>.statusBar.styleDefault(); this<\/span>.splashScreen.hide(); }); }\n<\/code><\/pre>\nThe Html code:<\/p>\n
<ion-header<\/span>><\/span> <ion-toolbar<\/span> color<\/span>=\"primary\"<\/span>><\/span> <ion-title<\/span>><\/span> Ionic Rxweb Reactive Form Validators <\/ion-title<\/span>><\/span> <\/ion-toolbar<\/span>><\/span>\n<\/ion-header<\/span>><\/span>\n<ion-content<\/span>><\/span> <ion-card<\/span>><\/span> <ion-card-content<\/span>><\/span> <form<\/span> [formGroup<\/span>]=\"userFormGroup\"<\/span> novalidate<\/span> (ngSubmit<\/span>)=\"userFormGroupSubmit()\"<\/span>><\/span> <ion-list<\/span>><\/span> <ion-item-divider<\/span> color<\/span>=\"primary\"<\/span>><\/span>User Profile Info<\/ion-item-divider<\/span>><\/span> <br<\/span>><\/span> <ion-item-divider<\/span> color<\/span>=\"light\"<\/span>><\/span> <ion-label<\/span>><\/span> General <\/ion-label<\/span>><\/span> <\/ion-item-divider<\/span>><\/span> <ion-item<\/span> [class.has-error<\/span>]=\"(userFormGroup.get('firstName').hasError('required') || userFormGroup.get('firstName').hasError('required')) && userFormGroup.get('firstName').touched\"<\/span>><\/span> <ion-label<\/span> position<\/span>=\"floating\"<\/span>><\/span>FirstName *<\/ion-label<\/span>><\/span> <ion-input<\/span> type<\/span>=\"text\"<\/span> formControlName<\/span>=\"firstName\"<\/span>><\/span><\/ion-input<\/span>><\/span> <\/ion-item<\/span>><\/span> <ion-item<\/span> lines<\/span>=\"none\"<\/span> *ngIf<\/span>=\"(userFormGroup.get('firstName').hasError('required') || userFormGroup.get('firstName').hasError('required')) && userFormGroup.get('firstName').touched\"<\/span>><\/span> <div<\/span> class<\/span>=\"error-message\"<\/span>><\/span> <small<\/span> *ngIf<\/span>=\"userFormGroup.get('firstName').hasError('required')\"<\/span>><\/span> {{ userFormGroup.controls.firstName.errors.required.message}}.<br<\/span> \/><\/span><\/small<\/span>><\/span> <\/div<\/span>><\/span> <\/ion-item<\/span>><\/span> <ion-item<\/span> [class.has-error<\/span>]=\"(userFormGroup.get('lastName').hasError('required') || userFormGroup.get('lastName').hasError('required')) && userFormGroup.get('lastName').touched\"<\/span>><\/span> <ion-label<\/span> position<\/span>=\"floating\"<\/span>><\/span>LastName *<\/ion-label<\/span>><\/span> <ion-input<\/span> type<\/span>=\"text\"<\/span> formControlName<\/span>=\"lastName\"<\/span>><\/span><\/ion-input<\/span>><\/span> <\/ion-item<\/span>><\/span> <ion-item<\/span> lines<\/span>=\"none\"<\/span> *ngIf<\/span>=\"(userFormGroup.get('lastName').hasError('required') || userFormGroup.get('lastName').hasError('required')) && userFormGroup.get('lastName').touched\"<\/span>><\/span> <div<\/span> class<\/span>=\"error-message\"<\/span>><\/span> <small<\/span> *ngIf<\/span>=\"userFormGroup.get('lastName').hasError('required')\"<\/span>><\/span> {{ userFormGroup.controls.lastName.errors.required.message}}.<br<\/span> \/><\/span><\/small<\/span>><\/span> <\/div<\/span>><\/span> <\/ion-item<\/span>><\/span> <ion-item<\/span> [class.has-error<\/span>]=\"(userFormGroup.get('Email').hasError('required') || userFormGroup.get('Email').hasError('required')) && userFormGroup.get('Email').touched\"<\/span>><\/span> <ion-label<\/span> position<\/span>=\"floating\"<\/span>><\/span>Email *<\/ion-label<\/span>><\/span> <ion-input<\/span> type<\/span>=\"email\"<\/span> formControlName<\/span>=\"Email\"<\/span>><\/span><\/ion-input<\/span>><\/span> <\/ion-item<\/span>><\/span> <ion-item<\/span> lines<\/span>=\"none\"<\/span> *ngIf<\/span>=\"(userFormGroup.get('Email').hasError('required') || userFormGroup.get('Email').hasError('required')) && userFormGroup.get('Email').touched\"<\/span>><\/span> <div<\/span> class<\/span>=\"error-message\"<\/span>><\/span> <small<\/span> *ngIf<\/span>=\"userFormGroup.get('Email').hasError('required')\"<\/span>><\/span>{{ userFormGroup.controls['Email'].errors.required.message}}.<br<\/span> \/><\/span><\/small<\/span>><\/span> <\/div<\/span>><\/span> <\/ion-item<\/span>><\/span> <ion-row<\/span>