npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

ngx-translation-validation

v15.0.0

Published

> Automatic validation messages for Angular forms in any language

Downloads

80

Readme

Translation validation

Automatic validation messages for Angular forms in any language

GitHub Release MIT commitizen PRs styled with prettier

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

After this, you will need to register the providers using provideNgxTv()

This command will import the NgxTvModule.forRoot() in your AppModule:

import { provideNgxTv } from 'ngx-translation-validation';

bootstrapApplication(AppComponent, {
  providers: [provideNgxTv(/* Optionally you can provide a config here **/)],
});

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

[!NOTE]
If the parent of the control is a FormArray, the control name will be set to the name of the parent of the FormArray.

formBuilder.group({
  name: ['', Validators.required], // When the control is in a group, use the key of the group, controlName -> name
  company: formBuilder.group({
    address: ['', Validators.required], // Nesting of groups is ignored, only direct parent is considered, controlName -> address
  }),
  cars: formBuilder.array([
    formBuilder.group({
      model: ['', Validators.required], // Group nested in array, only direct parent is considered, controlName -> model
    }),
  ]),
  colorPreferences: formBuilder.array([
    ['', Validators.required], // Control nested in array, controlName is name of array, controlName -> colorPreferences
  ]),
});

If the name of a control cannot be found, a ControlNameNotFoundError will be thrown, no validation error will be shown!

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 to the forRoot() method.

import { provideNgxTv } from 'ngx-translation-validation';

bootstrapApplication(AppComponent, {
  providers: [
    provideNgxTv({
      type: 'validation', // default 'validation'
      defaultScope: 'general', // default 'general'
      invalidClass: 'invalid-input', // default undefined
      submittedClass: 'form-submitted', // default 'ng-submitted'
      errorsComponent: SomeErrorContainerComponent, // default NgxTvContainerComponent
    }),
  ],
});
  • 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 version | Angular version | | :-------------: | :-------------: | | 14.x | >16 | | 13.x | >=13 <16 | | 1.x | 12.x |