[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 HTML forms. Form validators not only help you to get better quality data but they also guide the user through your form. Angular comes with a series of built-in validators such as required or maxLength etc. But very soon you have to build your own custom validators to handle more complex scenarios. In this lesson you‘re going to learn how to create such custom validators for Angular‘s reactive forms.

Basic validator is just a function.

import { ValidatorFn, AbstractControl, ValidationErrors } from ‘@angular/forms‘;

export function nameValidator(name: string): ValidatorFn {
  return (control: AbstractControl): ValidationErrors | null => {
    const isValid = control.value === ‘‘ || control.value === name;

    if (isValid) {
      return null;
    } else {
      return {
        nameMatch: {
          allowedName: name
        }
      };
    }
  };
}

Then you can use it with the validation in Reactvie form:

   import { nameValidator } from ‘./name.validator‘;

    this.form = this.fb.group({
      firstname: [‘‘, [Validators.required, nameValidator(‘someone‘)]]
    });
时间: 2024-10-12 15:27:38

[Angular] Create a custom validator for reactive forms in Angular的相关文章

[Angular2 Form] Angular 2 Template Driven Form Custom Validator

In this tutorial we are going to learn how we can also implement custom form field validation in Angular 2 template driven forms, by creating our own custom validation directive. We are going to see how to write such directive and how its a best prac

[Angular] Component architecture and Reactive Forms

It it recommeded that when deals with form component, we can create a container component to hold state, and then create a stateless component to enpower the form. For example: In the example has two components, one is container component 'meal.compo

[Angular2 Form] Model Driven Form Custom Validator

In this tutorial we are going to learn how simple it is to create custom form field driven validators while using Angular 2 model driven forms. These type of validators are really just plain functions that follow a set of conventions. We are going to

Reactive Forms

Angular 4.x 中有两种表单: Template-Driven Forms - 模板驱动式表单 (类似于 AngularJS 1.x 中的表单 ) Reactive Forms (Model-Driven Forms) - 响应式表单 Template-Driven Forms vs Reactive Forms Template-Driven Forms (模板驱动表单) 的特点 使用方便 适用于简单的场景 通过 [(ngModel)] 实现数据双向绑定 最小化组件类的代码 不易于单元

Vue——解决[Vue warn]: Invalid prop: custom validator check failed for prop "index". found in错误

Invalid prop: custom validator check failed for prop "index". 错误重现: 使用element-ui的菜单,在SubMenu Attribute中有一个index的属性,如果index的值从后端传入,则可能出现这个错误. 代码: 错误: 解决办法: index的值不能有空格,并且要为字符串类型,我上面的错误就是因为jc.eid是一个int型.将其转为字符串就不会报错了. 原文地址:https://www.cnblogs.com

Vue Element遇警告:[Vue warn]: Invalid prop: custom validator check failed for prop "type".

更详细的信息如下: [Vue warn]: Invalid prop: custom validator check failed for prop "type". found in ---> <ElBadge> at packages/badge/src/main.vue <ElMenuItem> at packages/menu/src/menu-item.vue <ElMenu> at packages/menu/src/menu.vue

[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: ['', Valida

[Angular] Create a ng-true-value and ng-false-value in Angular by controlValueAccessor

If you're coming from AngularJS (v1.x) you probably remember the ng-true-value and ng-false-value directive which allowed to map custom boolean values like "yes" or "no" or whatever other value you had, onto your HTML form. In this les

ng2响应式表单-翻译与概括官网REACTIVE FORMS页面

本文将半翻译半总结的讲讲ng2官网的另一个未翻译高级教程页面. 原文地址. 文章目的是使用ng2提供的响应式表单技术快速搭出功能完善丰富的界面表单组件. 响应式表单是一项响应式风格的ng2技术,本文将解释响应式表单并用来创建一个英雄详情编辑器. 包含内容: 响应式表单介绍 开始搭建 创建数据模型 创建响应式的表单组件 创建组建的模板文件 引入ReactiveFormsModule 显示HeroDetailComponent 添加一个FormGroup 看看表单模型 介绍FormBuilder 验