[](https://dev.azure.com/ajayojha/rxweb/_build/latest?definitionId=39&branchName=master) [](https://git
npm install @rxweb/reactive-form-validators





@rxweb/reactive-form-validators@13.x |
@rxweb/reactive-form-validators@13.x |
bash
$ npm install @rxweb/reactive-form-validators
`
Import modules
To work on form it is require to import angular modules(FormsModule and ReactiveFormsModule) and also import RxReactiveFormsModule which provides advanced/dynamic level validation features. All three modules register in the NgModule decorator imports property.
`js
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { FormsModule, ReactiveFormsModule} from '@angular/forms'; // <-- #1 import module
import { RxReactiveFormsModule } from '@rxweb/reactive-form-validators'; // <-- #2 import module
import {AppComponent} from './app.component';
@NgModule({
declarations:[AppComponent],
imports:[ BrowserModule,
FormsModule,
ReactiveFormsModule,
RxReactiveFormsModule
]
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
`
Form Validation
Three ways to apply validation on the form via Validators, Validation Decorators or Template Form.
1. allOf
2. alpha
3. alphaNumeric
4. ascii
5. choice
6. compare
7. compose
8. contains
9. creditCard
10. dataUri
11. different
12. digit
13. email
14. endsWith
15. even
16. extension
17. factor
18. file
19. fileSize
20. greaterThanEqualTo
21. greaterThan
22. ip
23. image
24. hexColor
25. json
26. latitude
27. latLong
28. leapYear
29. lessThanEqualTo
30. lessThan
31. longitude
32. lowerCase
33. mac
34. maxDate
35. maxLength
36. maxNumber
37. minDate
38. minLength
39. minNumber
40. noneOf
41. numeric
42. odd
43. oneOf
44. password
45. pattern
46. port
47. primeNumber
48. range
49. required
50. startsWith
51. time
52. unique
53. upperCase
54. url
allOf
allOf validation will check whether the user has entered all of the values of given field or not.
> Reactive Form Validation
`js
this.employeeInfoFormGroup = this.formBuilder.group({
skills:[RxwebValidators.allOf({matchValues:["MVC", "AngularJS","AngularJS 5","C#"]})],
});
`
> Template Form Validation
`html
[(ngModel)]="employee.skills" [allOf]='{"matchValues":"MVC","AngularJS","AngularJS","C#"}'/>{{tag.name}}
`
> Decorator Based Validation
`js
@allOf({matchValues: ["MVC", "AngularJS","Angular 5","C#"]}) skills: string[];
`
___
alpha
Alpha validation will allow only alphabets to be entered. It will not allow any digit or special character.
> Reactive Form Validation
`js
this.countryFormGroup = this.formBuilder.group({
countryName:['', RxwebValidators.alpha()],
});
`
> Template Form Validation
`html
[(ngModel)]="country.countryName" alpha >
`
> Decorator Based Validation
`js
@alpha() countryName: string;
`
___
alphaNumeric
Alpha Numeric validation will allow only alphabets and numbers to be entered, It will not allow any special character.
> Reactive Form Validation
`js
this.locationFormGroup = this.formBuilder.group({
areaName:['', RxwebValidators.alphaNumeric()],
});
`
> Template Form Validation
`html
[(ngModel)]="location.areaName" alphaNumeric >
`
> Decorator Based Validation
`js
@alphaNumeric() areaName: string;
`
___
ascii
ascii validation decorator allows user to enter the input which is in the proper ascii format.
> Reactive Form Validation
`js
this.userFormGroup = this.formBuilder.group({
specialCharAsciiCode:['', RxwebValidators.ascii()],
});
`
> Template Form Validation
`html
[(ngModel)]="user.specialCharAsciiCode" ascii >
`
> Decorator Based Validation
`js
@ascii() specialCharAsciiCode: string;
`
___
choice
choice validation will check whether the value entered is matching the properties defined.
> Reactive Form Validation
`js
this.employeeInfoFormGroup = this.formBuilder.group({
skills:[RxwebValidators.choice({minLength:5})],
});
`
> Template Form Validation
`html
[(ngModel)]="employee.skills" [choice]='{"minLength":"5"}'/>{{tag.name}}
`
> Decorator Based Validation
`js
@choice({minLength:'5'}) skills: string[];
`
___
compare
Compare validation will compare two inputs whether they are same or not.
> Reactive Form Validation
`js
this.userFormGroup = this.formBuilder.group({
password:['',],
confirmPassword:['', RxwebValidators.compare({fieldName:'password' })],
});
`
> Template Form Validation
`html
[(ngModel)]="user.confirmPassword" [compare]='{"fieldName":"password"}'] >
`
> Decorator Based Validation
`js
@compare({fieldName:'password'}) confirmPassword: string;
`
___
compose
Compose validation decorator is used to apply multiple validations on a particular field.
> Reactive Form Validation
`js
this.userForm = this.formBuilder.group({
Username:['',[ RxwebValidators.compose({
validators:[
RxwebValidators.required(),
RxwebValidators.alpha()
],]]
});
`
> Template Form Validation
`html
[(ngModel)]="user.Username" [compose]='{"validators":[alpha,required]}' >
`
> Decorator Based Validation
`js
@compose({validators:[alpha,required]}) Username: string;
`
___
contains
Contains validation will check that value is in the input, It will not allow to enter input that not contains the predefined value.
> Reactive Form Validation
`js
this.userFormGroup = this.formBuilder.group({
emailAddress:['', RxwebValidators.contains({value:'@gmail.com' })],
});
`
> Template Form Validation
`html
[(ngModel)]="user.emailAddress" [contains]='{"fieldName":"@gmail.com"}' >
`
> Decorator Based Validation
`js
@contains({value:'@gmail.com'}) emailAddress: string;
`
___
creditCard
creditCard validation will check property value is creditcardtype or not, It will not allow to enter any value other than credit card format.
> Reactive Form Validation
`js
this.userFormGroup = this.formBuilder.group({
cardType:['',],
creditCardNumber:['', RxwebValidators.creditCard({fieldName:'cardType' })],
});
`
> Template Form Validation
`html
[(ngModel)]="user.creditCardNumber" [creditCard]='{"fieldName":"cardType"}' >
`
> Decorator Based Validation
`js
@creditCard({fieldName:'cardType' }) creditCardNumber: string;
`
___
dataUri
dataUri validation allows user to enter the input which is a valid data Uri.
> Reactive Form Validation
`js
this.userFormGroup = this.formBuilder.group({
htmlDataUri:['', RxwebValidators.dataUri()],
});
`
___
> Template Form Validation
`html
[(ngModel)]="user.htmlDataUri" dataUri >
`
> Decorator Based Validation
`js
@dataUri() htmlDataUri: string;
`
___
different
Different validation will check two inputs whether they are different or not. It is just opposite of compare decorator.
> Reactive Form Validation
`js
this.accountInfoFormGroup = this.formBuilder.group({
firstName:['',],
lastName:['', RxwebValidators.different({fieldName:"firstName" })],
});
`
> Template Form Validation
`html
[(ngModel)]="account.lastName" [different]='{"fieldName":"firstName"}' >
`
> Decorator Based Validation
`js
@different({fieldName:"firstName" }) lastName: string;
`
___
digit
Digit validation will allow only digits to be entered, It will not allow any alphabets or special character.
> Reactive Form Validation
`js
this.userFormGroup = this.formBuilder.group({
age:['', RxwebValidators.digit()],
});
`
> Template Form Validation
`html
[(ngModel)]="user.age" digit >
`
> Decorator Based Validation
`js
@digit() age: number;
`
___
email
Email validation will only allow user to enter input which is in the correct email format.
> Reactive Form Validation
`js
this.userFormGroup = this.formBuilder.group({
email:['', RxwebValidators.email()],
});
`
> Template Form Validation
`html
[(ngModel)]="user.email" email >
`
> Decorator Based Validation
`js
@email() email: string;
`
___
endsWith
endsWith validation allows user to enter the input which ends with particular value.
> Reactive Form Validation
`js
this.userFormGroup = this.formBuilder.group({
name:['', RxwebValidators.endsWith({value:'m' })],
});
`
> Template Form Validation
`html
[(ngModel)]="user.name" [endsWith]='{"value":"m"}' >
`
> Decorator Based Validation
`js
@endsWith({value:'m' }) name: string;
`
___
even
Even validation will check whether the value entered by user is an even number or not.
> Reactive Form Validation
`js
this.userFormGroup = this.formBuilder.group({
evenNumber:['', RxwebValidators.even()],
});
`
> Template Form Validation
`html
[(ngModel)]="user.evenNumber" even >
`
> Decorator Based Validation
`js
@even() evenNumber: number;
`
___
extension
extension validation allows user to enter the input which is in the proper extension format.
> Reactive Form Validation
`js
this.userFormGroup = this.formBuilder.group({
Image :['', RxwebValidators.extension({extensions:'png','jpg'})],
});
`
> Template Form Validation
`html
[(ngModel)]="user.Image" [extension]='{"extensions":"png":"jpg"}' >
`
> Decorator Based Validation
`js
@extension({extensions:'png','jpg'}) Image: string;
`
___
factor
factor validation will allow user to enter factor of a number which is called dividend.
> Reactive Form Validation
`js
this.userFormGroup = this.formBuilder.group({
firstNumber:['', RxwebValidators.factor({dividend:50 })],
});
`
> Template Form Validation
`html
[(ngModel)]="user.firstNumber" [factor]='{"dividend":"50"}' >
`
> Decorator Based Validation
`js
@factor({dividend:50 }) firstNumber: Number;
`
___
file
file validation validators allows user to validate whether how many files can be uploaded . It depends upon maxFiles and minFiles.
> Reactive Form Validation
`js
this.userInfoFormGroup = this.formBuilder.group({
totalDocumentFiles:['', RxwebValidators.file({minFiles:5 })],
});
`
> Template Form Validation
`html
`
> Decorator Based Validation
`js
@file({maxFiles:5 })
totalImageFiles: number;
`
___
fileSize
fileSize validation allows user to enter the input which is in the proper file size format.
> Reactive Form Validation
`js
this.storageCapacityFormGroup = this.formBuilder.group({
videoStorageSize:['', RxwebValidators.fileSize({maxSize:50 })],
});
`
> Template Form Validation
`html
[(ngModel)]="storage.videoStorageSize" [fileSize]='{"maxSize":"50"}' >
`
> Decorator Based Validation
`js
@fileSize({maxSize:50 }) videoStorageSize: string;
`
___
greaterThanEqualTo
Greater than equal to validation decorator will check that input property is greater than or equal to the related field input.
> Reactive Form Validation
`js
this.userFormGroup = this.formBuilder.group({
age:['',],
voterAge:['', RxwebValidators.greaterThanEqualTo({fieldName:'age' })],
});
`
> Template Form Validation
`html
[(ngModel)]="user.voterAge" [greaterThanEqualTo]='{"fieldName":"age"}' >
`
> Decorator Based Validation
`js
@greaterThanEqualTo({fieldName:'age' }) voterAge: number;
`
___
greaterThan
Greater than validation will check that input property is greater than related field input.
> Reactive Form Validation
`js
this.userFormGroup = this.formBuilder.group({
age:['',],
voterAge:['', RxwebValidators.greaterThan({fieldName:'age' })],
});
`
> Template Form Validation
`html
[(ngModel)]="user.voterAge" [greaterThan]='{"fieldName":"age"}' >
`
> Decorator Based Validation
`js
@greaterThan({fieldName:'age' }) voterAge: number;
`
___
hexColor
HexColor validation will allow user to enter only the input in proper Hex Color format.
> Reactive Form Validation
`js
this.userFormGroup = this.formBuilder.group({
color:['', RxwebValidators.hexColor()],
});
`
> Template Form Validation
`html
[(ngModel)]="user.color" hexColor >
`
> Decorator Based Validation
`js
@hexColor() color: string;
`
___
ip
ip validation validators is used to validate the ip address of the device.
> Reactive Form Validation
`js
this.userFormGroup = this.formBuilder.group({
ipV4:['', RxwebValidators.ip({version:1 })],
});
`
___
image
image validation validators allows user to validate image like height,width etc .
> Reactive Form Validation
`js
this.userInfoFormGroup = this.formBuilder.group({
profilePhoto:['', RxwebValidators.image({maxHeight:100 ,maxWidth:100 })],
});
`
> Template Form Validation
`html
`
> Decorator Based Validation
`js
@image({maxHeight:100 ,maxWidth:100 })
profilePhoto: string;
`
___
json
json validation will allow user to enter the input only in proper Json format.
> Reactive Form Validation
`js
this.jsonInfoFormGroup = this.formBuilder.group({
locationJson:['', RxwebValidators.json()],
});
`
> Template Form Validation
`html
[(ngModel)]="json.locationJson" json >
`
> Decorator Based Validation
`js
@json() locationJson: string;
`
___
latitude
latitude validation allows user to enter value which is valid latitude.
> Reactive Form Validation
`js
this.numberInfoFormGroup = this.formBuilder.group({
firstCountryLatitude:['', RxwebValidators.latitude()],
});
`
> Template Form Validation
`html
[(ngModel)]="number.firstCountryLatitude" latitude >
`
> Decorator Based Validation
`js
@latitude() firstCountryLatitude: string;
`
___
leapYear
LeapYear validation will check whether the value entered is a leap year or not.
> Reactive Form Validation
`js
this.userFormGroup = this.formBuilder.group({
birthYear:['', RxwebValidators.leapYear()],
});
`
> Template Form Validation
`html
[(ngModel)]="user.birthYear" leapYear >
`
> Decorator Based Validation
`js
@leapYear() birthYear: number;
`
___
latLong
latLong validation allows user to enter the input which is valid Latitude or longitude.
> Reactive Form Validation
`js
this.countryFormGroup = this.formBuilder.group({
firstCountry:['', RxwebValidators.latLong()],
});
`
> Template Form Validation
`html
[(ngModel)]="country.firstCountry" latLong >
`
> Decorator Based Validation
`js
@latLong() firstCountry: string;
`
___
lessThanEqualTo
Less than equal to validation will allow the user to enter only that value which is less than oe equal to the value in the pre defined field.
> Reactive Form Validation
`js
this.userFormGroup = this.formBuilder.group({
totalMarks:['',],
marks:['', RxwebValidators.lessThanEqualTo({fieldName:'totalMarks' })],
});
`
> Template Form Validation
`html
[(ngModel)]="user.marks" [lessThanEqualTo]='{"fieldName":"totalMarks"}' >
`
> Decorator Based Validation
`js
@lessThanEqualTo({fieldName:'totalMarks' }) marks: number;
`
___
lessThan
Less than validation will allow the user to enter only that value which is less than the value in the pre defined field.
> Reactive Form Validation
`js
this.userFormGroup = this.formBuilder.group({
marks:['',],
passingMarks:['', RxwebValidators.lessThan({fieldName:'marks' })],
});
`
> Template Form Validation
`html
[(ngModel)]="user.passingMarks" [lessThan]='{"fieldName":"marks"}' >
`
> Decorator Based Validation
`js
@lessThan({fieldName:'marks' }) passingMarks: number;
`
___
longitude
longitude validation allows user to enter the input which is in the proper longitude format.
> Reactive Form Validation
`js
this.numberInfoFormGroup = this.formBuilder.group({
firstCountryLongitude:['', RxwebValidators.longitude()],
});
`
> Template Form Validation
`html
[(ngModel)]="number.firstCountryLongitude" longitude >
`
> Decorator Based Validation
`js
@longitude() firstCountryLongitude: string;
`
___
lowercase
lowerCase validation will allow user to enter only the lowercase alphabets.
> Reactive Form Validation
`js
this.userInfoFormGroup = this.formBuilder.group({
username:['', RxwebValidators.lowerCase()],
});
`
> Template Form Validation
`html
[(ngModel)]="user.username" lowercase >
`
> Decorator Based Validation
`js
@lowerCase() username: string;
`
___
mac
mac validation will check whether the value entered is a valid mac address.
> Reactive Form Validation
`js
this.macAddressInfoFormGroup = this.formBuilder.group({
systemMacAddress:['', RxwebValidators.mac()],
});
`
> Template Form Validation
`html
[(ngModel)]="macAddress.systemMacAddress" mac >
`
> Decorator Based Validation
`js
@mac() systemMacAddress: string;
`
___
maxDate
MaxDate validation will allow user to enter the date less than the maxDate value parameter.
> Reactive Form Validation
`js
this.userFormGroup = this.formBuilder.group({
registrationDate:['', RxwebValidators.maxDate({value:new Date(2018,7,30) })],
});
`
> Template Form Validation
`html
[(ngModel)]="user.registrationDate" [maxDate]='{"value":"2018-07-30"}' >
`
> Decorator Based Validation
`js
@maxDate({value:new Date(2018,7,30) }) registrationDate: Date;
`
___
maxLength
MaxLength validation will allow user to enter the input upto the maximum length value parameter.
> Reactive Form Validation
`js
this.locationFormGroup = this.formBuilder.group({
firstName:['', RxwebValidators.maxLength({value:10 })],
});
`
> Template Form Validation
`html
[(ngModel)]="location.firstName" [maxLength]='{"value":"10"}' >
`
> Decorator Based Validation
`js
@maxLength({value:10 }) firstName: string;
`
___
maxNumber
MaxNumber validation will allow user to enter the input upto the maximum number value parameter.
> Reactive Form Validation
`js
this.subjectDetailsFormGroup = this.formBuilder.group({
passingMarks:['', RxwebValidators.maxNumber({value:50 })],
});
`
> Template Form Validation
`html
[(ngModel)]="subject.passingMarks" [maxNumber]='{"value":"50"}' >
`
> Decorator Based Validation
`js
@maxNumber({value:50 }) passingMarks: number;
`
___
minDate
Minimum Date validation will allow user to enter date greater the minimum date value parameter.
> Reactive Form Validation
`js
this.userFormGroup = this.formBuilder.group({
registrationDate:['', RxwebValidators.minDate({value:new Date(2018,7,30) })],
});
`
> Template Form Validation
`html
[(ngModel)]="user.registrationDate" [minDate]='{"value":"2018-07-30"}' >
`
> Decorator Based Validation
`js
@minDate({value:new Date(2018,7,30) }) registrationDate: Date;
`
___
minLength
MinLength validation will allow user to enter the input length matching the minimum length value parameter.
> Reactive Form Validation
`js
this.contactFormGroup = this.formBuilder.group({
countryName:['', RxwebValidators.minLength({value:8 })],
});
`
> Template Form Validation
`html
[(ngModel)]="country.countryName" [minLength]='{"value":"8"}' >
`
> Decorator Based Validation
`js
@minLength({value:8 }) countryName: string;
`
___
minNumber
MinNumber validation will allow user to enter the input greater than the minimum number value parameter.
> Reactive Form Validation
`js
this.resultInfoFormGroup = this.formBuilder.group({
maths:['', RxwebValidators.minNumber({value:35 })],
});
`
___
> Template Form Validation
`html
[(ngModel)]="result.maths" [minNumber]='{"value":"35"}' >
`
> Decorator Based Validation
`js
@minNumber({value:35 }) maths: number;
`
___
noneOf
noneOf validation will check whether the user has entered none of the value is selected from the given inputs.
> Reactive Form Validation
`js
this.employeeInfoFormGroup = this.formBuilder.group({
skills:[RxwebValidators.noneOf({matchValues:["MVC", "AngularJS","Angular 5","C#"]})],
});
`
> Template Form Validation
`html
[(ngModel)]="employee.skills" [noneOf]='{"matchvalues":"MVC","AngularJS","Angular 5","C#"}'/>{{tag.name}}
`
> Decorator Based Validation
`js
@noneOf({matchValues: ["MVC", "AngularJS","Angular 5","C#"]}) skills: string[];
`
___
numeric
numeric validation will check whether the value entered is a valid numberic value or not.The validation can be set according to requirement, it can be either decimal,negative number or positive number.
> Reactive Form Validation
`js
this.userInfoFormGroup = this.formBuilder.group({
integerNumber:['', RxwebValidators.numeric({acceptValue:NumericValueType.PositiveNumber ,allowDecimal:false })],
});
`
> Template Form Validation
`html
[(ngModel)]="user.integerNumber" [numeric]='{"acceptValue":"NumericValueType.PositiveNumber","allowDecimal":"false"}' >
`
> Decorator Based Validation
`js
@numeric({acceptValue:NumericValueType.PositiveNumber ,allowDecimal:false }) integerNumber: number;
`
___
odd
Odd validation will check whether the value entered is an odd number or not.
> Reactive Form Validation
`js
this.userFormGroup = this.formBuilder.group({
oddNumber:['', RxwebValidators.odd()],
});
`
> Template Form Validation
`html
[(ngModel)]="user.oddNumber" odd >
`
> Decorator Based Validation
`js
@odd() oddNumber: number;
`
___
oneOf
oneOf validation will check whether the user has entered any one of the given inputs or not.
> Reactive Form Validation
`js
this.employeeInfoFormGroup = this.formBuilder.group({
skills:[RxwebValidators.oneOf({matchValues:["MVC", "AngularJS","Angular 5","C#"]})],
});
`
> Template Form Validation
`html
[(ngModel)]="employee.skills" [oneOf]='{"matchvalues":"MVC","AngularJS","Angular 5","C#"}'/>{{tag.name}}
`
> Decorator Based Validation
`js
@oneOf({matchValues: ["MVC", "AngularJS","Angular 5","C#"]}) skills: string[];
`
___
password
Password validation will allow user to enter only the input according to correct password validation format.
> Reactive Form Validation
`js
this.loginInfoFormGroup = this.formBuilder.group({
password:['', RxwebValidators.password({validation:{maxLength: 10,minLength: 5,digit: true,specialCharacter: true} })],
});
`
> Template Form Validation
`html
[(ngModel)]="login.password" [password]='{"validation":{maxLength: 10,minLength: 5,digit: true,specialCharacter: true}}' >
>
`
> Decorator Based Validation
`js
@password({validation:{maxLength: 10,minLength: 5,digit: true,specialCharacter: true} }) password: string;
`
___
pattern
Pattern validation will allow user to enter the input which match the predefined pattern value parameter.
> Reactive Form Validation
`js
this.userFormGroup = this.formBuilder.group({
userName:['', RxwebValidators.pattern({expression:{'onlyAlpha': /^[A-Za-z]+$/} })],
});
`
> Template Form Validation
`html
[(ngModel)]="user.userName" [pattern]='{"expression":{'onlyAlpha': /^[A-Za-z]+$/}}' >
`
> Decorator Based Validation
`js
@pattern({expression:{'onlyAlpha': /^[A-Za-z]+$/} }) userName: string;
`
___
port
port validation allows user to enter the input which is a valid port number.
> Reactive Form Validation
`js
this.userFormGroup = this.formBuilder.group({
educationalWebsitePort:['', RxwebValidators.port()],
});
`
> Template Form Validation
`html
[(ngModel)]="user.educationalWebsitePort" port >
`
> Decorator Based Validation
`js
@port() educationalWebsitePort: string;
`
___
primeNumber
primeNumber validation allows user to enter only prime number.
> Reactive Form Validation
`js
this.numberInfoFormGroup = this.formBuilder.group({
firstNumber:['', RxwebValidators.primeNumber()],
});
`
> Template Form Validation
`html
[(ngModel)]="user.firstNumber" primeNumber >
`
> Decorator Based Validation
`js
@primeNumber() firstNumber: string;
`
___
range
Range validation will check that the entered value is in the specified range
> Reactive Form Validation
`js
this.employeeInfoFormGroup = this.formBuilder.group({
age:['', RxwebValidators.range({minimumNumber:18 ,maximumNumber:60 })],
});
`
> Template Form Validation
`html
[(ngModel)]="employee.age" [range]='{"minimumNumber":"18","maximumNumber":"60"}' >
`
> Decorator Based Validation
`js
@range({minimumNumber:18 ,maximumNumber:60 }) age: number;
`
___
required
Required validation will check that the user has entered the value in the property or not.
> Reactive Form Validation
`js
this.userInfoFormGroup = this.formBuilder.group({
firstName:['', RxwebValidators.required()],
});
`
> Template Form Validation
`html
[(ngModel)]="user.firstName" required >
`
> Decorator Based Validation
`js
@required() firstName: string;
`
___
startsWith
startsWith validation allows user to enter the input which starts with particular value.
> Reactive Form Validation
`js
this.userFormGroup = this.formBuilder.group({
name:['', RxwebValidators.startsWith({value:'n' })],
});
`
> Template Form Validation
`html
[(ngModel)]="user.name" [startsWith]='{"value":"n"}'>
`
> Decorator Based Validation
`js
@startsWith({value:'n' }) name: string;
`
___
time
time validation will allow user to enter the input only in the correct time format.
> Reactive Form Validation
`js
this.attandanceDetailFormGroup = this.formBuilder.group({
entryTime:['', RxwebValidators.time()],
});
`
> Template Form Validation
`html
[(ngModel)]="attandance.entryTime" time >
`
> Decorator Based Validation
`js
@time() entryTime: string;
`
___
unique
Unique validation validators is used to validate unique input based on formArray.
> Reactive Form Validation
`js
this.employeeFormGroup = this.formBuilder.group({
fullName:[''],
skills:this.formBuilder.array([
this.getSkillFormGroup()
])
});
addSkill(){
let skillsArray = this.employeeFormGroup.controls.skills;
skillsArray.push(this.getSkillFormGroup());
}
getSkillFormGroup(){
return this.formBuilder.group({
skillName:['',RxwebValidators.unique()]
})
}
`
> Decorator Based Validation
`js
@unique()
skillName: string[];
`
___
upperCase
UpperCase validation will allow user to enter the alphabets only in the upperCase format.
> Reactive Form Validation
`js
this.locationFormGroup = this.formBuilder.group({
countryName:['', RxwebValidators.upperCase()],
});
`
> Template Form Validation
`html
[(ngModel)]="location.countryName" upperCase >
`
> Decorator Based Validation
`js
@upperCase() countryName: string;
`
___
url
Url validation will check that value entered in the property is in the correct url format or not.
> Reactive Form Validation
`js
this.webSiteInfoModelFormGroup = this.formBuilder.group({
adminWebsiteUrl:['', RxwebValidators.url()],
});
`
> Template Form Validation
`html
[(ngModel)]="website.adminWebsiteUrl" url >
`
> Decorator Based Validation
`js
@url() adminWebsiteUrl: string;
`
___
Sanitization
1. blacklist
2. ltrim
3. rtrim
4. toBoolean
5. toDate
6. toDouble
7. toInt
8. toString
9. trim
10. whitelist
blacklist
Remove the characters form the user entered value.
`js
@prop()
@blacklist('abc' )
freeText: string;
`
___
ltrim
Trim characters from the left-side of the input.
`js
@prop()
@ltrim()
freeText: string;
`
___
rtrim
Trim characters from the right-side of the input.
`js
@prop()
@rtrim()
freeText: string;
`
___
toBoolean
Convert the input to a boolean.
`js
@prop()
@toBoolean()
Active: boolean;
`
___
toDate
Convert the input to a date, or null if the input is not a date.
`js
@prop()
@toDate()
dob: Date;
`
___
toDouble
Convert the input to a float, or NAN if the input is not a float.
`js
@prop()
@toDouble()
amount: number;
`
___
toInt
Convert the input to an integer, or NAN if the input is not an integer.
`js
@prop()
@toInt()
amount: number;
`
___
toString
Convert the input to a string.
`js
@prop()
@toString()
freeText: string;
`
___
trim
Trim characters from the input.
`js
@prop()
@trim()
freeText: string;
`
___
whitelist
Remove characters that do not appear in the whitelist.
`js
@prop()
@whitelist('abc' )
freeText: string;
`
___
Dynamic Validation
Dynamic validation is used, when the validation rules will come from server side, means there is no static code on client side to manage the validation on reactive form.
Scenario : First Name field should accepts alphabets and that is configured on server side.
Below is the json for validation rule for firstName field. See the working code example on stackblitz
> dynamic-validation.json
`json
{
"firstName":{
"alpha":true
}
}
`
First of all retrieve the validation rules from server and pass it to the group method.
> user-info.component.ts
`js
import { Component, OnInit } from '@angular/core';
import { FormGroup } from "@angular/forms"
import { HttpClient } from '@angular/common/http';
import { RxFormBuilder, FormBuilderConfiguration } from '@rxweb/reactive-form-validators';
@Component({
selector: 'app-user-info-add',
templateUrl: './user-info-add.component.html'
})
export class UserInfoAddComponent implements OnInit {
userInfoFormGroup: FormGroup
constructor(
private formBuilder: RxFormBuilder, private http: HttpClient
) { }
ngOnInit() {
this.http.get('assets/dynamic-validation.json').subscribe( (dynamicValidationConfiguration:any) => {
this.userInfoFormGroup = this.formBuilder.group({
firstName:['John']
},
new FormBuilderConfiguration( {dynamicValidation: dynamicValidationConfiguration}
));
})
}
}
`
Conditional Validation
Some fields are required based on some other fields value, like if the firstName field contains the value of 'John' then the lastName field is required. see the working code example on stackblitz
`js
import { Component, OnInit } from '@angular/core';
import { FormGroup } from "@angular/forms"
import { RxFormBuilder, RxwebValidators } from '@rxweb/reactive-form-validators';
@Component({
selector: 'app-user-info-add',
templateUrl: './user-info-add.component.html'
})
export class UserInfoAddComponent implements OnInit {
userInfoFormGroup: FormGroup
constructor(
private formBuilder: RxFormBuilder
) { }
ngOnInit() {
this.userInfoFormGroup = this.formBuilder.group({
firstName:['',[RxwebValidators.required()]],
lastName:['',[RxwebValidators.required({conditionalExpression:(x)=> x.firstName == 'John'})]]
});
}
}
`
Dirty Check
Check the form is dirty or not. On component side you have to create a FormGroup object via RxFormBuilder, afterthat you can use isDirty method from FormGroup object. See the dirty code example on stackblitz
`html
required Validator once for all properties
`
Post as Form Data of Reactive Form value
This provides a toFormData() method which converts the FormGroup value into the FormData. Here is an example of Post as FormData of Reactive Form value. See the working code example on stackblitz
> Reactive Form Validation
`js
this.userFormGroup = this.formBuilder.group({
firstName:[''],
lastName :[''],
userName:[''],
password : ['']
});
addUser(){
let formdata = this.userFormGroup.toFormData()
this.http.post(this.api, formdata); // This is fake uri, This is just for your reference.
}
`
Post Image through form Data
To create fileObject from the input we have to set writeFile="true" attribute on the HTMLInputFileElement. Here is an example of Post Image through formData. See the working code example on stackblitz
> Reactive Form Validation
`js
this.userInfoFormGroup = this.formBuilder.group({
profilePhoto:[''],
});
addUser(){
let formdata = (this.userInfoFormGroup).toFormData()
this.http.post(this.api, formdata); // This is fake uri, This is just for your reference.
}
`
`html
`
Reset Form
RxFormBuilder provide a solution for reset form of angular reactive form object. If you want to reset the form as per the value initialized while creating an instance of the formControls, you can use resetForm()method of FormGroupExtension. See the working code example on stackblitz
> Reactive Form Validation
`js
this.userFormGroup = this.formBuilder.group({
firstName:[''],
lastName:[''],
userName:[''],
password:['']
});
resetForm(){
(this.userFormGroup).resetForm();
}
`
Compare Password
Compare validation is used to check whether the value of two formControls are same or not .Here is an example of comparing password.
field. See the working code example on stackblitz
> Reactive Form Validation
`js
this.userFormGroup = this.formBuilder.group({
password:['',],
confirmPassword:['', RxwebValidators.compare({fieldName:'password' })],
});
`
> Template Form Validation
`html
[(ngModel)]="user.confirmPassword" [compare]='{"fieldName":"password"}'] >
`
> Decorator Based Validation
`js
@compare({fieldName:'password'}) confirmPassword: string;
`
Single Error Message
You can a single default message for a formControl and display it in single errormessage without multiple conditions stackblitz
`html
{{userFormGroup.controls.userName["errorMessage"]}}
`
In ReactiveFormConfig set the global validation message
` ReactiveFormConfig.set({
"validationMessage":{
"required":"This field is required",
"minLength":"minimum length is {{0}}",
"maxLength":"allowed max length is {{0}}"
}
});
this.userFormGroup = this.formBuilder.group({
userName:['',[RxwebValidators.required(),RxwebValidators.minLength({value:5}),RxwebValidators.maxLength({value:10})]]
})
``