Automatic validation messages for Angular forms in any language
We got tired of having to write code to display validation messages whenever a form control was invalid. So instead we wrote a library that automatically injects a component displaying the error message.
✅ Custom error component
✅ Show validation message on change, blur or submit
✅ Translates using Transloco
From your project folder, run:
yarn add ngx-translation-validation
or
npm install --save ngx-translation-validation
Also add transloco to your project if you haven't done so yet. Add transloco to your project
This command will import the NgxTvModule.forRoot()
in your AppModule
:
import { NgxTvModule } from 'ngx-translation-validation';
@NgModule({
declarations: [AppComponent],
imports: [NgxTvModule.forRoot()],
bootstrap: [AppComponent],
})
export class AppModule {}
To get started simply import the NgxTvModule
in the module of the component you want to have validation messages.
import { NgxTvModule } from 'ngx-translation-validation';
@NgModule({
declarations: [UserComponent],
imports: [NgxTvModule],
bootstrap: [UserComponent],
})
export class UserModule {}
And then every [formControlName] or [formControl] element will automatically show validation messages when it is invalid.
<form [formGroup]="form">
<div class="form-group">
<label for="name">Name</label>
<input formControlName="name" id="name" type="text">
</div>
</form>
To show the validation in the current language of the application we use Transloco by default. It translates a key to a string in the current language of the app. The keys for the validation generates by this packages have the following form:
type.scope.controlName.error;
type
- Name of the validation messages type. Useful for specifying a directory for the validation messages (default: validation)scope
- Group several validation messages to a specific form/group (default: general)controlName
- Name of the control on which the validation error occurserror
- The name of the validation error that occurs
Examples
validation.general.name.required;
validation.general.email.email;
validation.organizationForm.name.required;
To customize the behavior of the plugin you can pass an object the the forRoot()
method.
import { NgxTvModule } from 'ngx-translation-validation';
@NgModule({
declarations: [AppComponent],
imports: [
NgxTvModule.forRoot({
type: 'validation', // default 'validation'
defaultScope: 'general', // default 'general'
invalidClass: 'invalid-input', // default undefined
submittedClass: 'form-submitted', // default 'ng-submitted'
errorsComponent: SomeErrorContainerComponent, // default NgxTvContainerComponent
}),
],
bootstrap: [AppComponent],
})
export class AppModule {}
invalidClass
- Class that is added to a formControl element when the control is invalid. Can be used for custom styling when you do not want to useng-invalid
(default: undefined)submittedClass
- Class that is added to the formGroup element when it is submitted. Can be used in combination with ng-invalid to style elements when invalid and the form is submitted (default: 'ng-submitted')errorsComponent
- Component in which the error message is rendered. If you want to use a custom component, please sure to extend the default (default:NgxTvContainerComponent
)
To specify to what scope validation messages belong you can use the ngxTvScope
directive.
By default the ngx-tv-container-component gets injected right after the formControl element. So you html would look something like this:
<input type="text" /> <ngx-tv-container-component></ngx-tv-container-component>
But if you'd like the container to appear in a parent of the controlForm you could specify the location of the container with the ngxTvContainer
directive.
<form [formGroup]="form" ngxTvContainer>
<div class="form-group">
<label for="name">Name</label>
<input formControlName="name" id="name" type="text">
</div>
<button type="submit">Submit</button>
</form>
<ngx-tv-container-component></ngx-tv-container-component> // container component will be rendered here
We welcome you to contribute. This can be done by reporting issues or feature requests. Even better is to not only report issues or feature requests, but then also to pick those issues up yourself.
Please read CONTRIBUTING.md for details on the process for submitting pull requests to us.
Library version | Angular version |
---|---|
13.x | 13.x |
1.x | 12.x |