[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 HTML forms. Form validators not only help you to get better quality data but they also guide the user through your form. Angular comes with a series of built-in validators such as required or maxLength etc. But very soon you have to build your own custom validators to handle more complex scenarios. In this lesson you're going to learn how to create such custom validators for Angular's reactive forms.
Basic validator is just a function.
import { ValidatorFn, AbstractControl, ValidationErrors } from '@angular/forms';
export function nameValidator(name: string): ValidatorFn {
return (control: AbstractControl): ValidationErrors | null => {
const isValid = control.value === '' || control.value === name;
if (isValid) {
return null;
} else {
return {
nameMatch: {
allowedName: name
}
};
}
};
}
Then you can use it with the validation in Reactvie form:
import { nameValidator } from './name.validator';
this.form = this.fb.group({
firstname: ['', [Validators.required, nameValidator('someone')]]
});
[Angular] Create a custom validator for reactive forms in Angular的更多相关文章
- [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之响应式表单 ( Reactive Forms )
项目结构 一 首页 ( index.html ) <!doctype html> <html lang="en"> <head> <met ...
- [Angular] Create a custom pipe
For example we want to create a pipe, to tranform byte to Mb. We using it in html like: <div> ...
- [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 ...
- Angular Reactive Forms -- Model-Driven Forms响应式表单
Angular 4.x 中有两种表单: Template-Driven Forms - 模板驱动式表单 (类似于 AngularJS 1.x 中的表单 ) 官方文档:https://v2.angul ...
- [Angular] Component architecture and Reactive Forms
It it recommeded that when deals with form component, we can create a container component to hold st ...
- [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 ...
- Forms in Angular 2
Input handling is an important part of application development. The ng-model directive provided in A ...
- Part 13 Create a custom filter in AngularJS
Custom filter in AngularJS 1. Is a function that returns a function 2. Use the filter function to cr ...
随机推荐
- GIMP类似于PhotoShop的开源免费软件
首先我们先看看他的界面如何,都有哪些功能!而且它支持多种平台,可以在MacOS.Windows.Linux操作系统上使用.非常值得推荐! 1.官方地址下载地址: https://www.gimp. ...
- 个人学习源码的 HBase误区的总结 与 架构图
HDFS 的备份功能不是给 基于 HBase 等 基于HDFS 的项目做备份的. 如果 HBase 需要备份,那么久需要设置 备份(快照 )功能. HMaster . kafka 等无主结构并 ...
- Docker入门实践(三) 基本操作
Docker安装完毕.我们就能够试着来执行一些命令了.看看docker能够干什么. (一) 创建一个容器 首先.让我们执行一个最简单的容器,hello-world.假设安装没有问题.并执行正确的话,应 ...
- C++对象模型——效率有了,弹性呢(第七章)
7.4 效率有了,弹性呢 传统的C++对象模型提供有效率的运行期支持.这份效率,再加上与C之间的兼容性,造成了C++的广泛被接受度.然而,在某些领域方面,像是动态共享函数库(dynamicall ...
- Codeforces 11B Jumping Jack(数学)
B. Jumping Jack time limit per test 1 second memory limit per test 64 megabytes input standard input ...
- 小贝_php+redis类型组合使用
php_redis类型组合使用 一.类型组合说明 经过前面的文章介绍.已经知道redis有字符串.集合.列表.hash等内置数据类型. 这里以,无序集合为例,进行说明. 集合 set1的简图 1.从简 ...
- quartz-misfire 错失、补偿执行
调度(scheduleJob)或恢复调度(resumeTrigger,resumeJob)后不同的misfire对应的处理规则 misfire产生的条件是:到了该触发执行时上一个执行还未完成,且线程池 ...
- Python(五) 包、模块、函数与变量作用域
一.while循环与使用场景 CONDITION=1 while CONDITION <=5 : CONDITION +=1 print("hello") else: pri ...
- Nginx 代理以及HTTPS (二)
一.HTTPS解析 https 加密 私钥 公钥 http 的握手 是确认网络是连通的. https 的握手 是一个加密的过程 加密图 二. 使用Nginx 部署HTTPS 服务 1.证书生成命令(h ...
- 添加 validate 验证规则
上篇文章链接:http://blog.csdn.net/chenmoimg_/article/details/71191476 修改 msg.js $.extend($.validator.messa ...