[Angular] Create custom validators for formControl and formGroup
Creating custom validators is easy, just create a class inject AbstractControl.
Here is the form we want to validate it:
form = this.fb.group({
store: this.fb.group({
branch: ['', [Validators.required, StockValidators.checkBranch]],
code: ['', Validators.required]
}),
selector: this.createStock({}),
stock: this.fb.array([])
}, {validator: StockValidators.checkStockExist});
We put two custom validators into this form, one is for formControl
StockValidators.checkBranch
Another is for formGroup:
{validator: StockValidators.checkStockExist}
Notice that for formControl validators, it takes array of validator.
For formGroup, it take object.
And here is the validators, it is important that make static methods, so we don't need to new the class instance:
import {AbstractControl} from '@angular/forms';
export class StockValidators {
static checkBranch(control: AbstractControl) {
const branch = control.value;
const regex = /^[a-z]\d{}/i;
return regex.test(branch) ? null: {branchCheck: true};
} static checkStockExist(control: AbstractControl) {
const selector = control.get('selector').value;
const selectedItems = control.get('stock').value; if(!selector && !selectedItems) return null; const exist = selectedItems.some((stock) => Number(stock.product_id) === Number(selector.product_id));
return exist ? {stockExist: true}: null;
}
}
When check on HTML side, we can create a helper function to make DOM looks shorter:
<div
class="stock-selector__error"
*ngIf="stockExists">
Item already exists in the stock
</div>
get stockExists() {
return (
this.parent.hasError('stockExist') &&
this.parent.get('selector.product_id').dirty
);
}
[Angular] Create custom validators for formControl and formGroup的更多相关文章
- [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 ...
- [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 ...
- How to Create Custom Filters in AngularJs
http://www.codeproject.com/Tips/829025/How-to-Create-Custom-Filters-in-AngularJs Introduction Filter ...
- [转]How to Create Custom Filters in AngularJs
本文转自:http://www.codeproject.com/Tips/829025/How-to-Create-Custom-Filters-in-AngularJs Introduction F ...
- How to: Create Custom Configuration Sections Using ConfigurationSection
https://msdn.microsoft.com/en-us/library/2tw134k3.aspx You can extend ASP.NET configuration settings ...
- create custom launcher icon 细节介绍
create custom launcher icon 是创建你的Android app的图标 点击下一步的时候,出现的界面就是创建你的Android的图标 Foreground: ” Foregro ...
- java中如何创建自定义异常Create Custom Exception
9.创建自定义异常 Create Custom Exception 马克-to-win:我们可以创建自己的异常:checked或unchecked异常都可以, 规则如前面我们所介绍,反正如果是chec ...
- [Angular] Reactive Form -- FormControl & formControlName, FormGroup, formGroup & formGroupName
First time dealing with Reactive form might be a little bit hard to understand. I have used Angular- ...
- [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> ...
随机推荐
- zendiscovery 的Ping机制
ping是集群发现的基本手段,通过在网络上广播或者指定ping某些节点获取集群信息,从而可以找到集群的master加入集群.zenDiscovery实现了两种凭机制:广播与单播.本篇将详细分析一些这M ...
- 洛谷P3954 成绩【民间数据】
题目背景 数据已修复 题目描述 牛牛最近学习了C++入门课程,这门课程的总成绩计算方法是: 总成绩=作业成绩×20%+小测成绩×30%+期末考试成绩×50% 牛牛想知道,这门课程自己最终能得到多少分. ...
- 在VS中设置比较和谐的字体和颜色的方法
作者:朱金灿 来源:http://blog.csdn.net/clever101 先在studiostyl.es网站选择你喜欢的字体方案,我个人比较喜欢这款: Humane Studio,注意在网页上 ...
- 虚拟机上的企业网络管理系统(cisco works 2000安装配置)
虚拟机上的企业网络管理系统 北京 李晨光 相关文章 Cisco Works 2000 网络管理软件安装.配置全过程 http://you.video.sina.com.cn/b/18168631-14 ...
- golang pipe
===============golang pipe============== package main import ( "fmt" "io" ) func ...
- Nodejs源代码分析之Path
今天介绍一下nodejs Path的源代码分析,Path的API文档在https://nodejs.org/dist/latest-v5.x/docs/api/path.html,使用相对简单,在AP ...
- GO语言学习(二十)Go 语言递归函数
Go 语言递归函数 递归,就是在运行的过程中调用自己. 语法格式如下: func recursion() { recursion() /* 函数调用自身 */ } func main() { recu ...
- C# 实现Ajax的方式总结
1JavaScript实现AJAX效果 2.AjaxPro实现AJAX应用 3.微软AJAX控件库开发AJAX 比如ScriptManager,updatePanel,timer等 4.jquery ...
- Hbase技术详细学习笔记
注:转自 Hbase技术详细学习笔记 最近在逐步跟进Hbase的相关工作,由于之前对Hbase并不怎么了解,因此系统地学习了下Hbase,为了加深对Hbase的理解,对相关知识点做了笔记,并在组内进行 ...
- cocos2d-x中六种持续性动作
持续性动作: (一) 位置变化动作 Move by to Jump by to (二) 属性变化动作 Scale by to Rotate by to Fade in out to Tint to b ...