[Angular2 Form] Angular 2 Template Driven Form Custom Validator
In this tutorial we are going to learn how we can also implement custom form field validation in Angular 2 template driven forms, by creating our own custom validation directive. We are going to see how to write such directive and how its a best practive to extract the validation function to a separate function, so that it can also be used for model driven validation.
We are going to learn how we can configure the template driven custom validator directive into the Angular 2 Forms mechanism, by plugging the directive into the dependency injection system using NG_VALIDATORS and forwardRef.
import {Validator, NG_VALIDATORS, FormControl} from "@angular/forms";
import {validateDuration} from "./validateDuration";
import {Directive, forwardRef} from "@angular/core"; export const MIN_LENGTH_VALIDATOR = {
provide: NG_VALIDATORS,
multi: true,
useExisting: forwardRef(() => DurationValidator)
}; // target and duration with ngModel
@Directive({
selector: '[duration][ngModel]',
providers: [MIN_LENGTH_VALIDATOR]
})
export class DurationValidator implements Validator {
validate(c: FormControl): {[key: string]:any} {
return validateDuration(c);
}
}
use:
Duration: <input type="number" name="duration" [(ngModel)]="duration" duration required>
[Angular2 Form] Angular 2 Template Driven Form Custom Validator的更多相关文章
- [Angular2 Form] Model Driven Form Custom Validator
In this tutorial we are going to learn how simple it is to create custom form field driven validator ...
- [Angular] Create a custom validator for template driven forms in Angular
User input validation is a core part of creating proper HTML forms. Form validators not only help yo ...
- [Angular] Create a custom validator for reactive forms in Angular
Also check: directive for form validation User input validation is a core part of creating proper HT ...
- jQuery Form 表单提交插件----Form 简介,官方文档,官方下载地址
一.jQuery Form简介 jQuery Form插件是一个优秀的Ajax表单插件,可以非常容易地.无侵入地升级HTML表单以支持Ajax.jQuery Form有两个核心方法 -- ajaxF ...
- jquery提交form表单插件jquery.form.js
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- {Django基础十之Form和ModelForm组件}一 Form介绍 二 Form常用字段和插件 三 From所有内置字段 四 字段校验 五 Hook钩子方法 六 进阶补充 七 ModelForm
Django基础十之Form和ModelForm组件 本节目录 一 Form介绍 二 Form常用字段和插件 三 From所有内置字段 四 字段校验 五 Hook钩子方法 六 进阶补充 七 Model ...
- Jquery组织Form表单提交之Form submission canceled because the form is not connected
有时候导出Excel时需要根据某些条件筛选数据,然后将数据通过NPOI生成Excel并导出.组织数据时可以通过放到一个表单中,某些场景是使用脚本(如:jquery)组织一个form(通过字符串拼接), ...
- 基于Django Form源码开发自定义Form组件
import copy import re class ValidateError(Exception): def __init__(self, detail): self.detail = deta ...
- [oldboy-django][2深入django]学生管理(Form)-- 添加(美化Form表单:通过form给前端标签添加属性)
1 在student_list添加一个a标签, <p><a href="/app01/add_student" class="btn btn-prima ...
随机推荐
- 用node.js从零开始去写一个简单的爬虫
如果你不会Python语言,正好又是一个node.js小白,看完这篇文章之后,一定会觉得受益匪浅,感受到自己又新get到了一门技能,如何用node.js从零开始去写一个简单的爬虫,十分钟时间就能搞定, ...
- 为ImageView设置背景图片(代码中)
仅仅需三行代码: Resources resources = getBaseContext().getResources(); Drawable imageDrawable = resources.g ...
- Android-Volley网络通信框架(二次封装数据请求和图片请求(包含处理请求队列和图片缓存))
1.回想 上篇 使用 Volley 的 JsonObjectRequest 和 ImageLoader 写了 电影列表的样例 2.重点 (1)封装Volley 内部 请求 类(请求队列,数据请求,图片 ...
- 用openssl生成含有中文信息的证书
openssl 支持 ASCII 和 UTF-8 两种编码,应该可以制作中文证书. 在生成证书签发申请时,当输入中文则 openssl 报错,这是因为当前输入的字符是 ANSI 本地编码格式,超出了 ...
- 欧洲的VPS 1天内收到几万次ssh端口访问,99%的访问量来自中国
欧洲的VPS 1天内收到几万次ssh端口访问,99%的访问量来自中国 前几天开了个欧洲的VPS,当备用的,没怎么用.就这样的VPS在1天之内也收到不少来自中国网民的见面礼 用了别人的一条命令: gre ...
- CISP/CISA 每日一题 14
CISA 每日一题(答) 自动无人值守运行(LIGHTS-OUT)优势:1.信息系统运行成本的遏制/减少:2.持续运行(24/7):3.减少系统错误和中断次数. I/O 控制人员负责保证:1.批处理信 ...
- Spring学习总结(6)——Spring之核心容器bean
一.Bean的基础知识 1.在xml配置文件中,bean的标识(id 和 name) id:指定在benafactory中管理该bean的唯一的标识.name可用来唯一标识bean 或给bean起别名 ...
- restcontroller和controller区别
http://www.cnblogs.com/softidea/p/5884772.html#undefined http://blog.csdn.net/blueheart20/article/de ...
- POJ——T 1006 Biorhythms
http://poj.org/problem?id=1006 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 138219 ...
- COGS——T 1265. [NOIP2012] 同余方程
http://cogs.pro/cogs/problem/problem.php?pid=1265 ★☆ 输入文件:mod.in 输出文件:mod.out 简单对比时间限制:1 s 内 ...