将多个验证器存储到Angular 8 Reactive FormBuilders中的常量中 [英] Store Multiple Validators into a Constant in Angular 8 Reactive FormBuilders

查看:76
本文介绍了将多个验证器存储到Angular 8 Reactive FormBuilders中的常量中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将多个验证器存储到一个常数中并在Reactive FormBuilder中使用? 以下将适用1个验证器. 存储并在Angular 8 Reactive Formbuilder中使用Regex Const Validator

How do I store Multiple Validators into a Constant and use in Reactive FormBuilder? The following will apply 1 validator. Store and Use Regex Const Validator in Angular 8 Reactive Formbuilder

export const ZipValidation = Validators.pattern(/^\d{1,5}$/);

'ZipCode': [null, [Validators.maxLength(16), ZipValidation]],

我们需要将整个多个数组存储为一个常数. 这是经度和纬度的多个验证器.

We need to store a whole multiple array into a constant. This is multiple validators for latitude and longitude.

Validators.maxLength(32),
Validators.min(-90),
Validators.max(90),
Validators.pattern(/^\d*\.?\d*$/)

我尝试执行以下操作,但不起作用

I tried doing the following, it is not working

export const LatitudeLongitudeValidator = [Validators.maxLength(32),Validators.min(-90),Validators.max(90),Validators.pattern(/^\d*\.?\d*$/)];

'latitude': [null, [LatitudeLongitudeValidator]],

推荐答案

您可以拥有所有这些验证的数组,然后使用传播运算符在验证程序中使用该数组,

You can have array of all these validations and then using spread operator use that array inside validator,

export const CustomValidation = [
    Validators.maxLength(32),
    Validators.min(-90),
    Validators.max(90),
    Validators.pattern(/^\d*\.?\d*$/)
]

在表单组中使用此数组,

Use this array in formgroup,

'formField': ['', [ ...CustomValidation ]],

Spread运算符在数组变量之前是3个句点....

Spread operator is 3 periods dots ... just before array variable.

这篇关于将多个验证器存储到Angular 8 Reactive FormBuilders中的常量中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆