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

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

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

  3. How to Create Custom Filters in AngularJs

    http://www.codeproject.com/Tips/829025/How-to-Create-Custom-Filters-in-AngularJs Introduction Filter ...

  4. [转]How to Create Custom Filters in AngularJs

    本文转自:http://www.codeproject.com/Tips/829025/How-to-Create-Custom-Filters-in-AngularJs Introduction F ...

  5. How to: Create Custom Configuration Sections Using ConfigurationSection

    https://msdn.microsoft.com/en-us/library/2tw134k3.aspx You can extend ASP.NET configuration settings ...

  6. create custom launcher icon 细节介绍

    create custom launcher icon 是创建你的Android app的图标 点击下一步的时候,出现的界面就是创建你的Android的图标 Foreground: ” Foregro ...

  7. java中如何创建自定义异常Create Custom Exception

    9.创建自定义异常 Create Custom Exception 马克-to-win:我们可以创建自己的异常:checked或unchecked异常都可以, 规则如前面我们所介绍,反正如果是chec ...

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

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

随机推荐

  1. WSDL生成dll

    --生成代理类wsdl /l:cs /n:OAWebService /out:D:OAWebService.cs D:\OAWebService.WSDL--生成dllcsc /out:D:OAWeb ...

  2. Cisco安全防护读书笔记之一Cisco系统设备协议漏洞

    650) this.width=650;" onclick='window.open("http://blog.51cto.com/viewpic.php?refimg=" ...

  3. HDU——T 3579 Hello Kiki

    http://acm.hdu.edu.cn/showproblem.php?pid=3579 Time Limit: 2000/1000 MS (Java/Others)    Memory Limi ...

  4. 使用dotcloud免费ssh

    使用dotcloud免费ssh https://www.dotcloud.com一个项目在线托管网站,注册后可以免费托管两个项目. 注册帐号,在ubuntu中执行下面命令,安装dotcloud环境 s ...

  5. Mybatis like查询的写法--转载

    原文地址:http://lavasoft.blog.51cto.com/62575/1386870 Mybatis like查询官方文档没有明确的例子可循,网上搜索了很多,都不正确. Mybatis ...

  6. JS/CSS 全屏幕导航 – 从上到下动画

    <!DOCTYPE HTML> <html> <head> <meta charset="utf-8"> <title> ...

  7. 图解String类型的不可变性及其原因

    1.String的不可变性 String s="abcd": 上面的语句定义了一个字符串变量s.该变量指向字符串"abcd",当初始化变量s时,会在堆中为s非配 ...

  8. css函数——calc()和attr()

    css也有函数?好吧,我孤陋寡闻了.这里记录一下学习情况. calc()函数 定义:用于动态计算长度值 支持版本:css3 运算符前后都需要保留一个空格,例如:width: calc(100% - 1 ...

  9. UVA 10125 - Sumsets(POJ 2549) hash

    http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...

  10. 微服务实践(五):微服务的事件驱动数据管理 - DockOne.io

    原文:微服务实践(五):微服务的事件驱动数据管理 - DockOne.io [编者的话]本文是使用微服务创建应用系列的第五篇文章.第一篇文章介绍了微服务架构模式,并且讨论了使用微服务的优缺点:第二和第 ...