
rxweb
Advanced, conditional and dynamic validation framework for faster and easier web development.
- Basic validations.
- Advance validations.
- Conditional validations.
- Dynamic validations.
- Configure application wide validation messages.
- Set per control custom validation message.
- Make
FormGroup
object with default values and control validations.
Prerequisites
Reactive Form Validators will work in angular projects.
Table of Contents
Introduction
Smart way to validate the forms with @rxweb/reactive-form-validators
. RxWeb Reactive Forms Validation provides several different approaches to validate your application's data. It supports client side validations with reactive form in angular and also manage to useful validation messages.
Documentation
Installation
$ npm install @rxweb/reactive-form-validators@1.1.2-beta.2
Validation Quick Start
To learn more about Rx Web powerful validation features, let us look at a complete example of validating a form and displaying the error messages back to the user.
Import modules
To work on form it is require to import angular modules(FormsModule && ReactiveFormsModule) and for rxweb reactive form validation to import 'RxReactiveFormsModule' module and register that in the NgModule decorator imports property.
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { FormsModule, ReactiveFormsModule} from '@angular/forms';
import { RxReactiveFormsModule } from '@rxweb/reactive-form-validators';
import {AppComponent} from './app.component';
@NgModule({
declarations:[AppComponent],
imports:[ BrowserModule,
FormsModule,
ReactiveFormsModule,
RxReactiveFormsModule
]
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
Configure Global Validation Messages
Apply global validation messages throughout the application, then configure the validation messages globaly(whenever application starts it will initialized automatically).
Below is the example to configure the validation messages in 'ReactiveFromConfig'.
import { Component, OnInit } from '@angular/core';
import { FormGroup } from '@angular/forms';
import { ReactiveFormConfig } from '@rxweb/reactive-form-validators';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: './app.component.css'
})
export class AppComponent implements OnInit {
constructor() { }
ngOnInit(): void
{
ReactiveFormConfig.set({
"validationMessage": {
"required": "this field is required.",
}
});
}
}
Implement Validation Decorators
user.model.ts
import {
propObject, prop, alphaNumeric, alpha, compare, contains, creditCard, CreditCardType, digit, email, greaterThanEqualTo,greaterThan, hexColor, json, lessThanEqualTo, lowerCase, maxLength,maxNumber,
minNumber, password, pattern, lessThan, range, required, time, upperCase, url, propArray, minLength
} from "@rxweb/reactive-form-validators";
import { UserAddress } from "./user-address.model";
import { Course } from "./course.model";
export class User{
@alpha()
userName: string;
@alphaNumeric()
areaCode: string;
@prop() oldPassword: string;
@compare({ fieldName: "oldPassword" })
newPassword: string;
@contains({ value: "Admin" })
roleName: string;
@creditCard({ creditCardTypes: [CreditCardType.Visa] })
creditCardNo: string;
@digit()
joiningAge: number;
@email()
email: string;
@greaterThan({ fieldName: 'joiningAge' })
retirementAge: number;
@greaterThanEqualTo({ fieldName: 'joiningAge' })
currentAge: number;
@hexColor()
teamColorCode: string;
@json()
json: string;
@prop()
currentExperience: number;
@lessThanEqualTo({ fieldName: 'currentExperience' })
minimumExperience: string;
@lessThan({ fieldName: "currentExperience" })
experience: string;
@lowerCase()
cityName: string;
@maxLength({ value: 10 })
mobileNumber: string;
@maxNumber({ value: 3 })
maximumBankAccount: string;
@minLength({ value: 8 })
landlineNo: string;
@minNumber({ value: 1 })
minimumBankAccount: string;
@password({ validation: { maxLength: 10, minLength: 5, alphabet: true } })
password: string;
@pattern({ pattern: { 'zipCode': /^\d{5}(?:[-\s]\d{4})?$/ } })
zipCode: string
@range({ minimumNumber: 18, maximumNumber: 60 })
eligiblityAge: number;
@required()
stateName: string;
@time()
entryTime: string;
@upperCase()
countryCode: string;
@url()
socialProfileUrl: string
@propObject(UserAddress) userAddress: UserAddress;
@propArray(Course) courses: Array<Course>;
}
user.address.model.ts
import { prop } from "@rxweb/reactive-form-validators";
export class UserAddress {
@prop() mobileNo: string;
}
course.model.ts
import { prop } from "@rxweb/reactive-form-validators";
export class Course {
@required() courseName:string;
}
user.component.ts
import { Component, OnInit } from '@angular/core';
import { RxFormBuilder } from '@rxweb/reactive-form-validators';
import { FormGroup } from '@angular/forms';
import { User } from "./user.model";
@Component({...})
export class UserComponent implements OnInit {
userFormGroup: FormGroup;
constructor(private formBuilder: RxFormBuilder) { }
ngOnInit() {
let user = new User();
user.currentExperience = 5;
user.userAddress = new UserAddress();
let course = new Course();
user.courses = new Array<Course>();
user.courses.push(course);
this.userFormGroup = this.formBuilder.formGroup(user);
}
}