[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> ...
随机推荐
- 19,tuple多元数组
#include <iostream> #include <tuple> using namespace std; void main() { char ch = 'a'; ; ...
- 使用缓存Memcache存储更新微信access token
关键字:Memcache access_token 更新 存储 7200 本文介绍如何使用缓存Memcache存储及更新 access token的方法. 一.Access Token access_ ...
- go-web编程之处理xml
摘抄自astaxie的开源书籍 build-web-application-with-golang 接下来的例子以下面XML描述的信息进行操作. <?xml version="1.0& ...
- enq: TX - row lock contention故障处理一则
一个非常easy的问题,之所以让我对这个问题进行总结.一是由于没我想象的简单,在处理的过程中遇到了一些磕磕碰碰,甚至绕了一些弯路.二是引发了我对故障处理时的一些思考. 6月19日,下午5点左右.数据库 ...
- HDU 1848(sg博弈) Fibonacci again and again
Fibonacci again and again Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Jav ...
- jquery中$(dom).each()和$(dom).length的使用
1.$(dom).each();在dom处理上用的比较多. $(selector).each(function(index,element){ //selector会遍历当前页面里所有匹配的jquer ...
- Codeforces Round 363 Div. 1 (A,B,C,D,E,F)
Codeforces Round 363 Div. 1 题目链接:## 点击打开链接 A. Vacations (1s, 256MB) 题目大意:给定连续 \(n\) 天,每天为如下四种状态之一: 不 ...
- vue 图片lazyload
今天看到我一醉哥的一篇朋友圈分享:<不如我们从头来过 | 掘金> 看完之后,百感交集,互联网的浪潮使创业公司如雨后春笋般崛起, 每一个初创公司都会有一群怀着美好愿景的小伙伴, 但是当寒冬来 ...
- sshfs 通过ssh 挂载远程目录
安装:yum -y install sshfs 挂载远程 ssh 文件系统: sshfs -p 1234 root@192.168.1.218:/home/ /mnt/ sshfs -p SSH端口 ...
- 微信小程序常见的UI框架/组件库总结
想要开发出一套高质量的小程序,运用框架,组件库是省时省力省心必不可少一部分,随着小程序日渐火爆,各种不同类型的小程序也渐渐更新,其中不乏一些优秀好用的框架/组件库. 1:WeUI 小程序–使用教程 h ...