[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 learn how to write a custom form validator and what the validating function needs to return in order to respect the Angular 2 form field validation contract.

Define a custom validator:

import {FormControl} from "@angular/forms";

export function validateDuration(ctrl:FormControl){

  const numValue = parseInt(ctrl.value);
  const valid = numValue < 10;

  return valid ? null : {
    validateDuration: {
      valid: false,
      message: "Duration should less than 10"
    }
  }
}

It just a function which return null or object, is it has error, it should return an object.

this.reactiveForm = fb.group({
      ...
      duration: [
        0,
        [
          Validators.required,
          //Validators.pattern(‘[0-9]+‘),
          validateDuration
        ]
      ],
      ...
    });

We add ‘validateDuration‘ into our validators array.

Use it:

  <div class="form-field">
    <label>Duration:</label>
    <input formControlName="duration">
    <div *ngIf="reactiveForm.controls.duration.errors?.validateDuration">
       {{reactiveForm.controls.duration.errors?.validateDuration.message}}
    </div>
  </div>
时间: 2024-12-24 10:59:37

[Angular2 Form] Model Driven Form Custom Validator的相关文章

[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

使用饿了么ui表单验证报错: [Element Warn][Form]model is required for validat

[Element Warn][Form]model is required for validat 如文末的完整例子: 该提示说的是 form表单需要一个绑定一个 对象(使用:model="editform" 不能使用v-model="editform"), 同时v-model="multipleSelectionStudent" 的multipleSelectionStudent一定一定一定要在editform对象中, 而例子中v-model=

Model、Form、ModelForm

本节内容: 1:Model 2:Form 3:Model 1 2 3 http://www.cnblogs.com/wupeiqi/articles/6144178.html  武sir:Form组件 http://www.cnblogs.com/wupeiqi/articles/6216618.html  武sir:Model http://www.cnblogs.com/wupeiqi/articles/6229414.html  武sir:ModelForm Model ==> 强大的数据

[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

$(#form&#160;:input)与$(#form&#160;input)的区别

相信大家都很奇怪这两者的区别 我从两个方面简单介绍下 1. $("form :input") 返回form中的所有表单对象,包括textarea.select.button等    $("form input")返回form中的所有input标签对象 2. form input 是属于层级选择器(将每一个选择器匹配到的元素合并后一起返回)   form :input是属于表单选择器(匹配所有input,textarea,select,button等)

delphi form.hide和form.visiable 失效?

后来我发现form.hide和form.visiable 全部失效了,然后用 ShowWindow(Application.Handle, SW_HIDE);   ShowWindow(Application.MainFormHandle, SW_HIDE);

关于$(&quot;form&quot;).serializeObject()与$(&quot;form&quot;).serialize()

form.serialize():jQuery的serialize()方法通过序列化表单值,可以把序列化的值传给ajax()作为url的参数,轻松使用ajax()提交form表单了,而不需要一个一个获取表单中的值然后传给ajax() form.serializeObject():讲form表单转成javasrcipt object对象,将form里面的内容转化成json格式 例:{ "a":1, "b":2, "c":3 } 拓展:(1)将表单序

使用Form Builder创建Form具体步骤

使用Oracle Form Builder创建Form具体步骤 (Data Source为Table) 说明:当Block使用的Data Source为Table时,Form会自动Insert,Update,Delete,Lock.若要显示non-database Item,需在POST-QUERY Trigger 里手动写代码来为non-database Item取值. 步驟一:分析需求设计Table架构 1). Table需指定一Unique ID,可为其创建Unique Index,在Fo

Form.Show()与Form.ShowDialog()的区别

一 基本概念 首先,窗体和对话框的显示有两种方式:有模式&无模式. 有模式:显示重要消息的对话框应始终是有模式的,"模式"窗体或对话框必须关闭或隐藏,然后您才能继续使用应用程序的其余部分,例如 MessageBox 就是一个可供使用的有模式的窗体. 无模式:"无模式"窗体让您在此窗体与另一窗体之间变换焦点,而不必关闭初始窗体.用户在该窗体显示的同时可继续在任何应用程序的其他位置工作. 二 实现 1.将窗体显示为有模式对话框: Form.ShowDialog(