Reactive Form Custom Validation Errors
Light weight reactive form error display package.
This package will used to display the distinct error of your reactive forms control errors.
This will display both the default and custom errors,also you can customize the error messages of the default validators.
This project was generated with Angular CLI version 14.2.1.
Installation
npm install reactive-form-validation-error
Whats new !!
version : 0.0.6 - Bug fixed
- if there is no custom error object it will not show any issues. there is no need to pass the custom error object
Steps
- Import ReactiveFormCustomValidationLibModule
- Place the
reactiveFormCustomValidation-error
selector where you wanted to show the validation error - Based on your form pass the respective
@inputs
to the selector - If you are using the
mat-form-field
you have to place the selector inside themat-error
no need to specify anyother things - If you are not using
mat-form-field
you have to customize the style of the error message.
Usage
import ReactiveFormCustomValidationLibModule in your root or shared modules.
import {ReactiveFormCustomValidationLibModule} from 'reactive-form-validation-error'
@NgModule({
declarations: [],
imports: [ReactiveFormCustomValidationLibModule],
exports: [],
providers: [],
})
#In Html you can use the selector like below. In this selector type we have a diffrent type of inputs for the component.
Example. #Form with no nested formgroup
<reactiveFormCustomValidation-error [form]="loginForm" [controlName]="'email'" >
</reactiveFormCustomValidation-error>
#Form with nested formgroup
<reactiveFormCustomValidation-error [form]="sampleForm" [controlName]="'sampleFormControlName'"
[groupName] = "'sampleGroupName'" > </reactiveFormCustomValidation-error>
#Form with formArray
<reactiveFormCustomValidation-error [form]="sampleForm" [controlName]="'sampleFormControlName'"
[groupName] = "'sampleGroupName'" [arrayName] = "'sampleFormArrayName'" [groupIndex] = "sampleIndex" >
</reactiveFormCustomValidation-error>
Type of @Inputs for component
@Input('form') form!: FormGroup;
@Input('controlName') formControlName!: string;
@Input('groupName') formGroupName: string = '';
@Input('arrayName') formArrayName: string = '';
@Input('groupIndex') groupIndex!: number;
@Input ('customDefaultErrorMsg') customErrorMsgObj! : DefaultValidationErrorsModel;
Custom default validation errors
Below mentioned are the error message for the custom validators like email
min-length
etc..
export class ValidationErrorConstant {
static readonly REQUIRED = 'This field is required';
static readonly EMAIL = 'Please enter the valid email';
static readonly MIN = 'Value should be greater than';
static readonly MAX = 'Value should not be greater than';
static readonly MIN_LENGTH = 'Length should be greater than';
static readonly MAX_LENGTH = 'Length should not be greater than';
static readonly PATTERN = 'Invalid format/pattern';
}
You can able to customize the default validation error messages. For that you have to import this dataObject VO
import {DefaultValidationErrorsModel} from 'reactive-form-validation-error'
VO model
export interface DefaultValidationErrorsModel{
emailError?: string;
requiredError?: string;
minLengthError?: string;
maxLengthError?: string;
minError?: string;
maxError?: string;
patternError?: string;
}
set the object in your model. the fields are optional
customErrorMsg : DefaultValidationErrorsModel = {
requiredError : "sample custom error"
}
then pass it to selector like below
<reactiveFormCustomValidation-error [form]="loginForm" [controlName]="'email'" [customDefaultErrorMsg] = "customErrorMsg" >
</reactiveFormCustomValidation-error>
Contributing
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
Please make sure to update tests as appropriate.