[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 ...
随机推荐
- 【记录】无法读取配置节“AppSettings”,因为它缺少节声明
Web.config对大小写敏感, 把AppSettings改为appSettings即可.
- 小米开源文件管理器MiCodeFileExplorer-源码研究(3)-使用最多的工具类Util
Util.java,使用最广泛~代码中很多地方,都写了注释说明~基本不需要怎么解释了~ package net.micode.fileexplorer.util; import java.io.Fil ...
- ActiveMQ学习总结(7)——ActiveMQ使用场景
MQ简介: MQ全称为Message Queue, 消息队列(MQ)是一种应用程序对应用程序的通信方法.应用程序通过写和检索出入列队的针对应用程序的数据(消息)来通信,而无需专用连接来链接它们.消息传 ...
- JNI/NDK开发指南(九)——JNI调用性能測试及优化
转载请注明出处:http://blog.csdn.net/xyang81/article/details/44279725 在前面几章我们学习到了.在Java中声明一个native方法,然后生成本地接 ...
- 数组&对象
一.遍历数组的几种方式 var arr = [1,2,3]; Array.prototype.test=function(){} arr.name='jq' 1. for /* * inde ...
- 异步调用WCF的方法需要小心的地方
直接使用下面的代码,由于client对象占用的资源没有被释放,会导致内存泄露GetSimServiceReference.GetSimServiceClient client = new GetSim ...
- MYSQLMANAGER实例管理器总结
好久没有写文章了,今天来看看MYSQL的实例管理器(MYSQLMANAGER).一.简单介绍:1.MySQL实例管理器(IM)是通过TCP/IP端口运行的后台程序,用来监视和管理MySQL数据库服务器 ...
- NIO专栏学习
http://blog.csdn.net/column/details/12993.html
- CORS原理
http://blog.csdn.net/renfufei/article/details/51675148 https://html.spec.whatwg.org/multipage/infras ...
- gogodroid--android 上的IPV6工具
gogodroid--android 上的IPV6工具 系统需求是 Android 1.6以上的系统,已经root,能够执行modprobe命令(在终端里输入modprobe,如果显示了帮助便可以), ...