当前位置: 首页 > 软件库 > Web应用开发 > Web框架 >

ngx-errors

授权协议 MIT License
开发语言 JavaScript
所属分类 Web应用开发、 Web框架
软件类型 开源软件
地区 不详
投 递 者 闾丘鸣
操作系统 跨平台
开源组织
适用人群 未知
 软件概览

ngx-errors

A declarative validation errors module for reactive forms.




Overview

Why use ngx-errors, how to install and use.

What is it?

Declarative validation error messages for reactive forms.

Typically you'd do something like this:

<!-- without ngxErrors -->
<input type="text" formControlName="foo">

<div *ngIf="form.get('foo').hasError('required') && form.get('foo').touched">
  Field is required
</div>
<div *ngIf="form.get('foo').hasError('minlength') && form.get('foo').dirty">
  Min length is 5
</div>

With ngxErrors, we've taken a simple declarative approach that cleans up your templates to make validation easier:

<!-- with ngxErrors -->
<input type="text" formControlName="foo">

<div ngxErrors="foo">
  <div ngxError="required" when="touched">
    Field is required
  </div>
  <div ngxError="minlength" when="dirty">
    Min length is 5
  </div>
</div>

Check out the documentation below for all the syntax we provide.

Installation

yarn add @ultimate/ngxerrors

# OR

npm i @ultimate/ngxerrors

Setup

Just add ngx-errors to your module:

import { NgxErrorsModule } from '@ultimate/ngxerrors';

@NgModule({ imports: [ NgxErrorsModule ] })

Documentation

ngxErrors

The ngxErrors directive works by dynamically fetching your FormControl under-the-hood, so simply take your formControlName value and pass it into ngxErrors:

<input type="text" formControlName="username">
<div ngxErrors="username">
  // ...
</div>

This needs to be on a parent container that will encapsulate child ngxError directives.

ngxError

The ngxError directive takes either a string or array as arguments. The argument you pass in corresponds to any active errors exposed on your control, such as "required" or "minlength":

<input type="text" formControlName="username">
<div ngxErrors="username">
  <div ngxError="minlength">
    Min length is 5
  </div>
</div>

Note: when using array syntax, [] bindings are needed

Using the array syntax, when any condition in the array is true the error will be shown:

<input type="text" formControlName="username">
<div ngxErrors="username">
  <div [ngxError]="['minlength', 'maxlength']">
    Min length is 5, max length is 10
  </div>
</div>

ngxError #when

The when directive takes either a string or array as arguments. It allows you to specify when you wish to display the error based on the control state, such as "dirty" or "touched":

<input type="text" formControlName="username">
<div ngxErrors="username">
  <div ngxError="minlength" when="dirty">
    Min length is 5
  </div>
</div>

It also comes in array format for multiple rules:

<input type="text" formControlName="username">
<div ngxErrors="username">
  <div ngxError="minlength" [when]="['dirty', 'touched']">
    Min length is 5
  </div>
</div>

Dynamic errors

You can optionally data-bind and dynamically create validation errors with ngxErrors:

<input type="text" formControlName="username">
<div ngxErrors="person.username">
  <div *ngFor="let error of errors" [ngxError]="error.name" [when]="error.rules">
    {{ error.text }}
  </div>
</div>

With corresponding component class:

@Component({...})
export class MyComponent {
  errors = [
    { name: 'required', text: 'This field is required', rules: ['touched', 'dirty'] },
    { name: 'minlength', text: 'Min length is 5', rules: ['dirty'] }
  ];
}

Nested FormGroup support

ngxErrors also supports FormGroups with control names using dot notation:

<div formGroupName="person">
  <input type="text" formControlName="username">
  <div ngxErrors="person.username">
    <div ngxError="minlength" [when]="['dirty', 'touched']">
      Min length is 5
    </div>
  </div>
</div>

Exported Directive API

ngx-errors exports itself as ngxErrors, which means you can access information about your control error states elsewhere in your template using a template reference, such as #foo.

Basic usage:

<div ngxErrors="username" #myError="ngxErrors"></div>

getError(name: string): any;

The getError method returns the object associated with your error. This can be useful for dynamically displaying error rules.

Example: Adds Min length is 5 when value is less than 5 characters (based on Validators.minLength(5)).

<input type="text" formControlName="username">

<div ngxErrors="username" #myError="ngxErrors">
  <div ngxError="minlength" when="dirty">
    Min length should be {{ myError.getError('minlength')?.requiredLength }}
  </div>
</div>

The error returned is identical to Angular's FormControl API

hasError(name: string, conditions?: string | string[]): boolean;

The hasError method informs you if your control has the given error. This can be useful for styling elsewhere in your template based off the control's error state.

Example: Adds class="required" when "myError" has the required error.

<div [class.required]="myError.hasError('required')">
  <input type="text" formControlName="username">
</div>

<div ngxErrors="username" #myError="ngxErrors">
  <div ngxError="required" when="dirty">
    Field is required
  </div>
</div>

You can optionally pass in conditions in which to activate the error.

Example: Adds class="required" when "myError" has the required error and the states are 'dirty' and 'touched'.

<div [class.required]="myError.hasError('required', ['dirty', 'touched'])">
  <input type="text" formControlName="username">
</div>

<div ngxErrors="username" #myError="ngxErrors">
  <div ngxError="required" when="dirty">
    Field is required
  </div>
</div>

You can also use the "catch-all" selector to get the state of your entire control's validity, alongside on an optional state collection.

<div>
  <div [ngClass]="{
    invalid: myError.hasError('*'),
    invalidTouchedDirty: myError.hasError('*', ['touched', 'dirty'])
  }">
  </div>
  <input type="text" formControlName="username">
</div>

<div ngxErrors="username" #myError="ngxErrors">
  <div ngxError="required" when="dirty">
    Field is required
  </div>
</div>

isValid(name: string, conditions?: string | string[]): boolean;

The isValid method informs you if a your control is valid, or a property is valid. This can be useful for styling elsewhere in your template based off the control's validity state.

Example: Adds class="valid" when "myError" has no required error.

<div [class.valid]="myError.isValid('required')">
  <input type="text" formControlName="username">
</div>

<div ngxErrors="username" #myError="ngxErrors">
  <div ngxError="required" when="dirty">
    Field is required
  </div>
</div>

You can optionally pass in conditions in which to evaluate alongside the property you're checking is valid.

Example: Adds class="valid" when "myError" has no required error and the states are 'dirty' and 'touched'.

<div [class.valid]="myError.isValid('required', ['dirty', 'touched'])">
  <input type="text" formControlName="username">
</div>

<div ngxErrors="username" #myError="ngxErrors">
  <div ngxError="required" when="dirty">
    Field is required
  </div>
</div>

You can also use the "catch-all" selector to check if the control is valid, with no specific error properties, alongside on an optional state collection.

<div>
  <div [ngClass]="{
    valid: myError.isValid('*'),
    validTouchedDirty: myError.isValid('*', ['touched', 'dirty'])
  }">
  </div>
  <input type="text" formControlName="username">
</div>

<div ngxErrors="username" #myError="ngxErrors">
  <div ngxError="required" when="dirty">
    Field is required
  </div>
</div>

hasErrors: boolean;

The hasErrors property returns true if your control has any number of errors. This can be useful for styling elsewhere in your template on a global control level rather than individual errors.

Example: Adds class="errors" when "myError" has any errors.

<div [class.errors]="myError.hasErrors">
  <input type="text" formControlName="username">
</div>

<div ngxErrors="username" #myError="ngxErrors">
  <div ngxError="required" when="dirty">
    Field is required
  </div>
  <div ngxError="minlength" when="dirty">
    Min length is 5
  </div>
</div>

errors: { [key: string]: any; };

The errors property returns the object associated with any active errors. This can be used to access any error properties on your control.

Example: Adds Max length is 10, you typed (n) when value is more than 10 characters (based on Validators.maxLength(10)).

<input type="text" formControlName="username">

<div ngxErrors="username" #myError="ngxErrors">
  <div ngxError="minlength" when="dirty">...</div>
  <div ngxError="maxlength" when="dirty">...</div>
</div>

<div *ngIf="myError.errors?.maxlength">
  Max length is 10, you typed {{ myError.errors.maxlength.actualLength }}
</div>

The errors returned are identical to Angular's FormControl API

Contributing

Please see the contributing guidelines.

  • ngx_lua模块的原理: 1、每个worker(工作进程)创建一个Lua VM,worker内所有协程共享VM; 2、将Nginx I/O原语封装后注入 Lua VM,允许Lua代码直接访问; 3、每个外部请求都由一个Lua协程处理,协程之间数据隔离; 4、Lua代码调用I/O操作等异步接口时,会挂起当前协程(并保护上下文数据),而不阻塞worker; 5、I/O等异步操作完成时还原相关协程上下

  • ngx_lua模块的原理: 1、每个worker(工作进程)创建一个Lua VM,worker内所有协程共享VM; 2、将Nginx I/O原语封装后注入 Lua VM,允许Lua代码直接访问; 3、每个外部请求都由一个Lua协程处理,协程之间数据隔离; 4、Lua代码调用I/O操作等异步接口时,会挂起当前协程(并保护上下文数据),而不阻塞worker; 5、I/O等异步操作完成时还原相关协程上下

  • ngx_http_upstream_t数据结构的意义 typedef struct ngx_http_upstream_s ngx_http_upstream_t; struct ngx_http_upstream_s { // 处理读事件的回调函数 ngx_http_upstream_handler_pt read_event_handler; // 处理写

  • ngx_lua模块的原理: 1、每个worker(工作进程)创建一个Lua VM,worker内所有协程共享VM; 2、将Nginx I/O原语封装后注入 Lua VM,允许Lua代码直接访问; 3、每个外部请求都由一个Lua协程处理,协程之间数据隔离; 4、Lua代码调用I/O操作等异步接口时,会挂起当前协程(并保护上下文数据),而不阻塞worker; 5、I/O等异步操作完成时还原相关协程上下

  • nginx源码解析-ngx_strerror()/ngx_strerror_init() 将所有的错误码errno以及错误描述strerror(errno)提前存储到ngx_sys_errlist中,用于解决异步信号安全的问题。 /* * Copyright (C) Igor Sysoev * Copyright (C) Nginx, Inc. */ #include <ngx_confi

  • 目的 博主在树莓派4B上使用nginx+rtmp搭建音频直播流媒体服务器 安装步骤 参见博客:树莓派使用nginx+rtmp搭建音频直播流媒体服务器 编译时出错 编译时输入代码 make 出现以下错误 ...... ../nginx-rtmp-module-master/ngx_rtmp_eval.c: In function ‘ngx_rtmp_eval’: ../nginx-rtmp-mo

  • ngx_lua 模块提供的指令和API等: 指令名称 说明 lua_use_default_type 是否使用default_type指令定义的Content-Type默认值 lua_code_cache *_by_lua_file文件是否cache lua_regex_cache_max_entries lua_regex_match_limit lua_package_path 用Lua写的l

  • ngx_lua模块的原理: 1、每个worker(工作进程)创建一个Lua VM,worker内所有协程共享VM; 2、将Nginx I/O原语封装后注入 Lua VM,允许Lua代码直接访问; 3、每个外部请求都由一个Lua协程处理,协程之间数据隔离; 4、Lua代码调用I/O操作等异步接口时,会挂起当前协程(并保护上下文数据),而不阻塞worker; 5、I/O等异步操作完成时还原相关协程上下

  • lua ngx模块提供的指令和API: 转载:http://www.cnblogs.com/wangxusummer/p/4309007.html 指令名称 说明 lua_use_default_type 是否使用default_type指令定义的Content-Type默认值 lua_code_cache *_by_lua_file文件是否cac

  • ngx_lua模块的原理: 1、每个worker(工作进程)创建一个Lua VM,worker内所有协程共享VM; 2、将Nginx I/O原语封装后注入 Lua VM,允许Lua代码直接访问; 3、每个外部请求都由一个Lua协程处理,协程之间数据隔离; 4、Lua代码调用I/O操作等异步接口时,会挂起当前协程(并保护上下文数据),而不阻塞worker; 5、I/O等异步操作完成时还原相关协程上下

  • ngx_http_split_clients_module 示例配置 指令 split_clients ngx_http_split_clients_module 模块用于创建适用于 A/B 测试的变量,也称为拆分测试。 示例配置 http { split_clients "${remote_addr}AAA" $variant { 0.5%

  • 翻译自: ngx.ssl - Lua API for controlling NGINX downstream SSL handshakes 1. 概要 # 注意:如果你使用的是 OpenResty 1.9.7.2+,则不需要该行 lua_package_path "/path/to/lua-resty-core/lib/?.lua;;"; server { listen 443 ssl

 相关资料
  • ngx-weui 是一个使用 Angular 构建的 WeUI 组件。 在线示例以及API文档。

  • ngx-fastdfs 是 nginx + lua +fastdfs 实现分布式图片实时动态压缩。 install 进入docker目录docker build -t  fastdfs:dev . 使用 docker -idt -p 80:80 fastdfs:dev /bin/bash进入容器执行/etc/rc.local 测试 进入容器执行test目录下的./test.sh或者直接执行下面脚本

  • ngx-markdown ngx-markdown is an Angular library that combines... Marked to parse markdown to HTML Prism.js for language syntax highlight Emoji-Toolkit for emoji support KaTeX for math expression rende

  • ngx-admin Who uses ngx-admin?| Documentation | Installation Guidelines | Angular templates New! Material theme for ngx-admin Material admin theme is based on the most popular Angular dashboard templat

  • @sweetalert2/ngx-sweetalert2 Official SweetAlert2 integration for Angular This is not a regular API wrapper for SweetAlert (which already works very well alone), it intends to provide Angular-esque ut

  • ngx-dropzone A lightweight and highly customizable Angular dropzone component for file uploads. For a demo see DEMO. And the CODE for the demo. Install $ npm install --save ngx-dropzone Usage // in ap