All Projects → DmitryEfimenko → ngx-messages

DmitryEfimenko / ngx-messages

Licence: other
Angular directives for displaying validation messages similar to these from AngularJs

Programming Languages

typescript
32286 projects
javascript
184084 projects - #8 most used programming language
HTML
75241 projects
SCSS
7915 projects

Projects that are alternatives of or similar to ngx-messages

flask-pydantic
flask extension for integration with the awesome pydantic package
Stars: ✭ 181 (+964.71%)
Mutual labels:  validation
strickland
Strickland is a JavaScript validation framework with a focus on extensibility and composition
Stars: ✭ 16 (-5.88%)
Mutual labels:  validation
magic-bytes
A library for detecting file types.
Stars: ✭ 20 (+17.65%)
Mutual labels:  validation
osiris
🎨 A Vue.js 2.0 universal responsive UI component library
Stars: ✭ 36 (+111.76%)
Mutual labels:  ui-components
monalisa-ui
MonalisaUI ✨ React Native UI Library
Stars: ✭ 37 (+117.65%)
Mutual labels:  ui-components
tailwind-ui-components
Free Tailwind CSS UI Components - Crafted for modern websites, landing pages and web apps. TailGrids Core is free and open-source so, feel free to use with your personal or commercial projects. If you would like to show your support and love, don't forget to give us a star 🌟
Stars: ✭ 49 (+188.24%)
Mutual labels:  ui-components
ttv
A command line tool for splitting files into test, train, and validation sets.
Stars: ✭ 38 (+123.53%)
Mutual labels:  validation
valid-data-url
Detect if a string is a data URL
Stars: ✭ 17 (+0%)
Mutual labels:  validation
is-valid-domain
Validate domain name in JavaScript
Stars: ✭ 50 (+194.12%)
Mutual labels:  validation
SlidableImage
Fancy slider for before&after images
Stars: ✭ 32 (+88.24%)
Mutual labels:  ui-components
nexus-validate
🔑 Add argument validation to your GraphQL Nexus API.
Stars: ✭ 29 (+70.59%)
Mutual labels:  validation
validada
Another library for defensive data analysis.
Stars: ✭ 29 (+70.59%)
Mutual labels:  validation
sodium-ui
Sodium is a simple, modular and customizable web component library to build elegant and accessible UI pieces for your React Application.
Stars: ✭ 23 (+35.29%)
Mutual labels:  ui-components
apple-receipt
Apple InAppPurchase Receipt - Models, Parser, Validator
Stars: ✭ 25 (+47.06%)
Mutual labels:  validation
laravel-starter-kit
A minimal starter kit for Laravel 8 using Bootstrap 5 and Webpixels CSS.
Stars: ✭ 23 (+35.29%)
Mutual labels:  ui-components
fefe
Validate, sanitize and transform values with proper TypeScript types and zero dependencies.
Stars: ✭ 34 (+100%)
Mutual labels:  validation
js-form-validator
Javascript form validation. Pure JS. No jQuery
Stars: ✭ 38 (+123.53%)
Mutual labels:  validation
flutter json view
📄 Displaying json models in a Flutter widget
Stars: ✭ 28 (+64.71%)
Mutual labels:  ui-components
nice-alert.js
Makes alert box suck less
Stars: ✭ 38 (+123.53%)
Mutual labels:  ui-components
validate
Modern lightweight library without dependencies for the data validation from single input tag
Stars: ✭ 24 (+41.18%)
Mutual labels:  validation

ngx-messages

⚠️ For Angular version 9+ use @ngspot/ngx-errors

I solely missed ng-messages directive from AngularJs, so I created this one to use in Angular 2+. In contrast to the one from AngularJs, this one requires you to pass control to the directive, instead of its errors. This allowed me to hook in to the status of control, like its dirty state, and display validation messages according to that status. A nice side effect of that decision is less boilerplate code.

Installation

To install this library, run:

$ npm install ngx-messages --save

Consuming ngx-messages

// app.module.ts
import { NgModule } from "@angular/core";
import { BrowserModule } from "@angular/platform-browser";
import { NgxMessagesModule } from "ngx-messages"; // <-- import the module
import { MyComponent } from "./my.component";

@NgModule({
  imports: [
    BrowserModule,
    NgxMessagesModule, // <-- include it in your app module
  ],
  declarations: [MyComponent],
  bootstrap: [MyComponent],
})
export class MyAppModule {}
// my.component.ts
import { Component, OnInit } from "@angular/core";
import {
  FormBuilder,
  FormGroup,
  Validators,
  AbstractControl,
} from "@angular/forms";

@Component({
  selector: "my-component",
  template: `
    <form novalidate [formGroup]="myForm" (ngSubmit)="checkEmail()">
      <input formControlName="email" placeholder="Email" type="email" />
      <div val-messages="email">
        <span val-message="required">Please provide email address</span>
        <span val-message="server" useErrorValue="true"></span>
      </div>
      <button type="submit">Check email</button>
    </form>
  `,
})
export class MyComponent implements OnInit {
  myForm: FormGroup;

  constructor(private _fb: FormBuilder, private backendService: any) {}

  ngOnInit() {
    this.myForm = this._fb.group({
      email: ["", Validators.required],
    });
  }

  checkEmail() {
    this.backendService.checkEmail().subscribe((result) => {
      if (result.error) {
        // server returns actual message for the error.
        // Setting attribute `useErrorValue` allows to use it directly
        addError(this.myForm.get("email"), { server: result.error });
      }
    });
  }
}

function addError(control: AbstractControl, error: { [key: string]: any }) {
  const updatedErrors = Object.assign({}, control.errors, error);
  control.setErrors(updatedErrors);
}

By default, ngx-messages only show errors when input is dirty. Howeverm you can change that by configuring module during its import/declaration:

@NgModule({
  imports: [
    BrowserModule,
    NgxMessagesModule.configure({
      showErrorsOnlyIfInputDirty: false,
      showErrorsWhenFormSubmitted: true,
    }),
  ],
  declarations: [MyComponent],
  bootstrap: [MyComponent],
})
export class MyAppModule {}

Styling messages

Just include something similar to the following in your global css file:

[val-message] {
  color: red;
}

Development

To generate all *.js, *.js.map and *.d.ts files:

$ npm run tsc

To lint all *.ts files:

$ npm run lint

License

MIT © Dmitry Efimenko

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].