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. php课程 12-42 php中类的关键字有哪些

    php课程 12-42 php中类的关键字有哪些 一.总结 一句话总结:const.final.static 1.类常量-const2.最终版本-final3.静态成员-static 1.php中类常 ...

  2. 【单词】常见单词含义的辨异(emulator/simulator、hardware/firmware)

    1. emulator 与 simulator The Simulator tries to duplicate the behavior of the device.(仿真的是行为): The Em ...

  3. BZOJ3262: 陌上花开(三维偏序,CDQ分治)

    Description 有n朵花,每朵花有三个属性:花形(s).颜色(c).气味(m),用三个整数表示. 现在要对每朵花评级,一朵花的级别是它拥有的美丽能超过的花的数量. 定义一朵花A比另一朵花B要美 ...

  4. 多线程中的&quot;断点&quot;续传《notify()和wait()》

    眼下在做一个项目.关于软件管理与下载的,预计项目提交日期定在6月9号.项目做了有20天了,可是在一个功能上卡住了.在这个项目中有一个功能----APK的下载须要实现. 相信大家都玩过非常多关于下载AP ...

  5. layout-maxWidth属性用法

    对于maxWidth属性,相信大家都不陌生.不过,今天我遇到了一个问题,就是当我希望一个relayout的宽度有个最大值的时候,用maxWidth却没办法实现.这里总结下maxWidth 的用法 1. ...

  6. AndroidStudio MAT LeakCanary 内存分析之 LeakCanary

    现在我们换一种更清晰方便的方式:LeakCanary https://github.com/square/leakcanary 首先将LeakCanary绑在我们的app上 build.gradle ...

  7. AVCaptureSession音频视频采集

    // // AudioVideoCaptureViewController.m // live // // Created by lujunjie on 2016/10/31. // Copyrigh ...

  8. [selenium]选取下拉框内容的方法

    说明:本文章主要是对select元素操作的讲解,非select元素的下拉框需要另外分析 1.select元素示例: 2.select下拉框选取的3种方法 WebElement selector = d ...

  9. Android中Application类的详解:

    Android中Application类的详解: 我们在平时的开发中,有时候可能会须要一些全局数据.来让应用中的全部Activity和View都能訪问到.大家在遇到这样的情况时,可能首先会想到自定义一 ...

  10. Android车载导航的一些困境

    车载导航从最初的用解码芯片,过渡到用WinCE系统,已经形成了一个较大的产业.车载导航使用上的一些大原则,基本上被固定了下来.如今WinCE走到了尽头,Android车载导航開始发力,但由于Andro ...