[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 validators while using Angular 2 model driven forms. These type of validators are really just plain functions that follow a set of conventions.
We are going to learn how to write a custom form validator and what the validating function needs to return in order to respect the Angular 2 form field validation contract.
Define a custom validator:
import {FormControl} from "@angular/forms";
export function validateDuration(ctrl:FormControl){
const numValue = parseInt(ctrl.value);
const valid = numValue < ;
return valid ? null : {
validateDuration: {
valid: false,
message: "Duration should less than 10"
}
}
}
It just a function which return null or object, is it has error, it should return an object.
this.reactiveForm = fb.group({
...
duration: [
,
[
Validators.required,
//Validators.pattern('[0-9]+'),
validateDuration
]
],
...
});
We add 'validateDuration' into our validators array.
Use it:
<div class="form-field">
<label>Duration:</label>
<input formControlName="duration">
<div *ngIf="reactiveForm.controls.duration.errors?.validateDuration">
{{reactiveForm.controls.duration.errors?.validateDuration.message}}
</div>
</div>
[Angular2 Form] Model Driven Form Custom Validator的更多相关文章
- [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 Ang ...
- python笔记-20 django进阶 (model与form、modelform对比,三种ajax方式的对比,随机验证码,kindeditor)
一.model深入 1.model的功能 1.1 创建数据库表 1.2 操作数据库表 1.3 数据库的增删改查操作 2.创建数据库表的单表操作 2.1 定义表对象 class xxx(models.M ...
- 使用饿了么ui表单验证报错: [Element Warn][Form]model is required for validat
[Element Warn][Form]model is required for validat 如文末的完整例子: 该提示说的是 form表单需要一个绑定一个 对象(使用:model=" ...
- Model、Form、ModelForm的比较
Model.Form.ModelForm 本节内容: 1:Model 2:Form 3:Model Form 1 2 3 http://www.cnblogs.com/wupeiqi/articles ...
- Django之Model、Form、ModelForm区别
本节内容: 1:Model https://www.cnblogs.com/shuai1991/p/10844662.html 2:Form ...
- [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 ...
- day75 form 组件(对form表单进行输入值校验的一种方式)
我们的组件是什么呢 select distinct(id,title,price) from book ORM: model.py class Book(): title=model.CharFiel ...
- {Django基础十之Form和ModelForm组件}一 Form介绍 二 Form常用字段和插件 三 From所有内置字段 四 字段校验 五 Hook钩子方法 六 进阶补充 七 ModelForm
Django基础十之Form和ModelForm组件 本节目录 一 Form介绍 二 Form常用字段和插件 三 From所有内置字段 四 字段校验 五 Hook钩子方法 六 进阶补充 七 Model ...
- [Vue warn]: Invalid prop: custom validator check failed for prop "type".
遇到错误如下, [Vue warn]: Invalid prop: custom validator check failed for prop "type". found in ...
随机推荐
- csdn课堂学习
http://edu.csdn.net/course/detail/2495?ref=blog&loc=0 http://edu.csdn.net/course/detail/2140/336 ...
- 10小时之内,暴力破解SSH账号的IP
10小时之内,暴力破解SSH账号的IP,IP 地址数据来源于 ip138.com 182.18.76.246 北京市昌平区 北京亿安天下网络科技有限公司 联通 221.223.200.143 北京市 ...
- TextView -无法调节字体、边框的距离
今天调节一个字体边框距离,结果一直都实现不了,布局如下 <RelativeLayout xmlns:android="http://schemas.android.com/apk/re ...
- HTTP协议建立连接、通讯与关闭连接全过程
为解决服务器TimeWait多的问题,了解了一下TCP/IP协议的连接过程.以访问一静态页面为例,从建立连接到访问拿到数据,然后关闭的整个过程.使用EtherPeek截图如下: 图首为一次交互过程 ...
- http 协议上传文件multipart form-data boundary 说明--转载
原文地址:http://xixinfei.iteye.com/blog/2002017 含义 ENCTYPE="multipart/form-data" 说明: 通过 http 协 ...
- Day4下午解题报告
预计分数:30+30+0=60 实际分数:30+30+10=70 稳有个毛线用,,又拿不出成绩来,, T1 https://www.luogu.org/problem/show?pid=T15626 ...
- Day4上午解题报告
预计分数:50 +0+0=50 实际分数:50+0+10=60 毒瘤出题人,T3不给暴力分 (*  ̄︿ ̄) T1 https://www.luogu.org/problem/show?pid=T155 ...
- 【Codeforces Round #455 (Div. 2) B】Segments
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 处理出所有的线 其实就是区间. 总共有n*(n+1)/2个 然后按照左端点.右端点排序 每次取最左边的线. 多种可能就取右端点尽量小 ...
- 洛谷 P2819 图的m着色问题
P2819 图的m着色问题 题目背景 给定无向连通图G和m种不同的颜色.用这些颜色为图G的各顶点着色,每个顶点着一种颜色.如果有一种着色法使G中每条边的2个顶点着不同颜色,则称这个图是m可着色的.图的 ...
- 用VBS控制鼠标,在Excel2010、2013,64位中
原作者文章地址:http://demon.tw/programming/vbs-control-mouse.html 感谢原作者的攻略.才使我学会用VBS控制鼠标. 但是问题接踵而至,Excel200 ...