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的更多相关文章

  1. [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 ...

  2. python笔记-20 django进阶 (model与form、modelform对比,三种ajax方式的对比,随机验证码,kindeditor)

    一.model深入 1.model的功能 1.1 创建数据库表 1.2 操作数据库表 1.3 数据库的增删改查操作 2.创建数据库表的单表操作 2.1 定义表对象 class xxx(models.M ...

  3. 使用饿了么ui表单验证报错: [Element Warn][Form]model is required for validat

    [Element Warn][Form]model is required for validat 如文末的完整例子: 该提示说的是 form表单需要一个绑定一个 对象(使用:model=" ...

  4. Model、Form、ModelForm的比较

    Model.Form.ModelForm 本节内容: 1:Model 2:Form 3:Model Form 1 2 3 http://www.cnblogs.com/wupeiqi/articles ...

  5. Django之Model、Form、ModelForm区别

    本节内容: 1:Model               https://www.cnblogs.com/shuai1991/p/10844662.html 2:Form                 ...

  6. [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 ...

  7. day75 form 组件(对form表单进行输入值校验的一种方式)

    我们的组件是什么呢 select distinct(id,title,price) from book ORM: model.py class Book(): title=model.CharFiel ...

  8. {Django基础十之Form和ModelForm组件}一 Form介绍 二 Form常用字段和插件 三 From所有内置字段 四 字段校验 五 Hook钩子方法 六 进阶补充 七 ModelForm

    Django基础十之Form和ModelForm组件 本节目录 一 Form介绍 二 Form常用字段和插件 三 From所有内置字段 四 字段校验 五 Hook钩子方法 六 进阶补充 七 Model ...

  9. [Vue warn]: Invalid prop: custom validator check failed for prop "type".

    遇到错误如下, [Vue warn]: Invalid prop: custom validator check failed for prop "type". found in ...

随机推荐

  1. traceroute----追踪数据包在网络上的传输过程

    traceroute命令 用于追踪数据包在网络上的传输时的全部路径,它默认发送的数据包大小是40字节. traceroute通过发送小的数据包到目的设备直到其返回,来测量其需要多长时间.一条路径上的每 ...

  2. JavaScript学习总结(9)——JS常用函数(一)

    本文中,收集了一些比较常用的Javascript函数,希望对学习JS的朋友们有所帮助. 1. 字符串长度截取 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 1 ...

  3. 洛谷 P1400 塔

    P1400 塔 题目描述 有N(2<=N<=600000)块砖,要搭一个N层的塔,要求:如果砖A在砖B上面,那么A不能比B的长度+D要长.问有几种方法,输出 答案 mod 10000000 ...

  4. Android图片处理——压缩、剪裁、圆角、保存

    点击查看原文 项目中用到的关于图片的处理 public class UtilPicture { public static final String IMAGE_UNSPECIFIED = " ...

  5. Harry Potter and the Goblet of Fire

    书名:Harry Potter and the Goblet of Fire  作者:J.K. Rowling 篇幅: 752页 蓝思值:880L 用时:    17天 工具:  有道词典 [透析成果 ...

  6. struts2中action手动获取參数

    struts2中action手动获取Session,jsp页面參数 1. ActionContext 在Struts2开发中,除了将请求參数自己主动设置到Action的字段中,我们往往也须要在Acti ...

  7. 无阻塞加载外部js(动态脚本元素,XMLHttpRequest注入,LazyLoad)

    动态脚本元素即在js中去创建<script>标签加载外部js并执行,这样加载的好处是文件的下载和执行过程不会阻塞页面的其他进程.通过下面两个例子对比出效果 <!DOCTYPE htm ...

  8. Mybatis批量插入,是否能够返回id列表

    第1次代码 void batchAdd(List<Photo> list); <insert id="batchAdd" parameterType=" ...

  9. 【2017 Multi-University Training Contest - Team 7】Just do it

    [Link]:http://acm.hdu.edu.cn/showproblem.php?pid=6129 [Description] 设定b[i]=a[1]^a[2]^a[3]^------a[i] ...

  10. 基于mybatis的BaseDao及BaseService深度结合(转)

    原文地址:http://zhaoshijie.iteye.com/blog/2003209 关键字:Mybatis通用DAO设计封装(mybatis) 说明: mybatis默认分页机制为逻辑分页,所 ...