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. 3. CONFIGURATION官网剖析(博主推荐)

    不多说,直接上干货! 一切来源于官网 http://kafka.apache.org/documentation/ 3. CONFIGURATION 3.1 Broker Configs 3.2 Pr ...

  2. JavaFx EventHandler

    import javafx.application.Application; import javafx.event.ActionEvent; import javafx.event.EventHan ...

  3. 【2017 Multi-University Training Contest - Team 6】Kirinriki

    [链接]http://acm.hdu.edu.cn/showproblem.php?pid=6103 [题意] 给出一串字符串,从中选出两个不重叠的字符串,使得两个字符串的距离和 <= m 的最 ...

  4. MySQL 5.7 多实例安装部署实例

    1. 背景  MySQL数据库的集中化运维,可以通过在一台服务器上,部署运行多个MySQL服务进程,通过不同的socket监听不同的服务端口来提供各自的服务.各个实例之间是相互独立的,每个实例的dat ...

  5. PatentTips - Virtual machine management using processor state information

    BACKGROUND OF THE INVENTION The invention generally relates to virtual machine management, and more ...

  6. 11.typeid

    #include <iostream> using namespace std; void main() { int a; cout << typeid(a).name() & ...

  7. Flask项目之手机端租房网站的实战开发(八)

    说明:该篇博客是博主一字一码编写的,实属不易,请尊重原创,谢谢大家! 接着上一篇博客继续往下写 :https://blog.csdn.net/qq_41782425/article/details/8 ...

  8. SpringMVC之类型转换Converter

    (转载:http://blog.csdn.net/renhui999/article/details/9837897) 1.1     目录 1.1      目录 1.2      前言 1.3   ...

  9. JS实现放大镜效果(放大图片)

    注意:里边的两张图片(一大一小)可以自己添加,JQ采用jquery-1.11.3.js版,也可自行调换. HTML代码: <!DOCTYPE html> <html> < ...

  10. Git 基本使用方法

    Git有一个优点,在本地的每个项目都是一个完整的仓库,除了须要从网络拉取和推送到网络之外,其它全部的操作都能够在本地完毕. 本文简单地介绍怎样在本地使用Git来对文件进行管理,下一篇文章再来说一下分支 ...