Validation messages used by form controls of Angular.
- custom messages configuration
- Interpolate messages by Message parameters
To install this library, run:
$ npm install ngx-validation-messages --save
Choose the version corresponding to your Angular version:
Angular | version |
---|---|
6,7,8 | 2.x |
5 | 1.x |
4 | 0.x |
e.g.
@NgModule({
...
imports: [
NgxValidationMessagesModule.configure({
messages: {
required: '{name} is required.',
minlength: '{name} must be at least {min} characters long.'
}
})
],
...
})
'interpolate' method of NgxValidationMessagesService that return Array of validation message for each form
Example
{
"name":["name is required","name must be at least 5."],
"address":["address is required"]
}
<!-- app.component.html -->
<div class="container">
<form #form="ngForm">
<div class="form-group">
<label for="name">Name</label>
<input type="text" id="name" class="form-control" required name="name" [(ngModel)]="value">
<div *ngIf="formErrors && formErrors.name.length > 0" class="alert alert-danger">
<span *ngFor="let e of formErrors.name">
{{ e }}
</span>
</div>
</div>
</form>
</div>
'message' keyword of parameter is the message itself.
config = {
"name": {
required: { message: "Name is invalid." }
}
};