Socket
Socket
Sign inDemoInstall

ngx-translation-validation

Package Overview
Dependencies
5
Maintainers
1
Versions
12
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    ngx-translation-validation

> Automatic validation messages for Angular forms in any language


Version published
Weekly downloads
1.1K
decreased by-4.86%
Maintainers
1
Install size
158 kB
Created
Weekly downloads
 

Changelog

Source

13.0.2 (2022-07-19)

Bug Fixes

  • deps: update dependency @ngneat/transloco to v4.1.1 (c63f68b)
  • deps: update dependency zone.js to v0.11.6 (a27a737)

Readme

Source

Translation validation

Automatic validation messages for Angular forms in any language

GitHub Release MIT commitizen PRs styled with prettier All Contributors

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.

Features

✅ Custom error component
✅ Show validation message on change, blur or submit
✅ Translates using Transloco
⚠️ WIP: translates using ngx-translate

Table of Contents

Blog posts

Installation

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 {}

Usage

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>

Internationalization

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 occurs
  • error - The name of the validation error that occurs

Examples

validation.general.name.required
validation.general.email.email

validation.organizationForm.name.required

Configuration

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 use ng-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)
Configure scope

To specify to what scope validation messages belong you can use the ngxTvScope directive.

Styling

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

Contribute

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.

Versioning

Library versionAngular version
13.x13.x
1.x12.x

FAQ

How to ...

Nothing has been asked yet. Will update when the time is right.

Contributors ✨

Thanks goes to these wonderful people (emoji key):


Wybren Kortstra

💻 📖 🤔 🚧

This project follows the all-contributors specification. Contributions of any kind welcome!

Icons made by Freepik from www.flaticon.com

Keywords

FAQs

Last updated on 19 Jul 2022

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc