[Angular 2] Custom Validtors
Create a custom validtor which only accepts the string start with '123';
function skuValidator(control){
if(!control.value.match(/^123/)){
return {invalidSku: true};
}
}
If it not start with 123, then return the object {invalidSku: true}, which later will be used in the html.
To use this validtor:
this.myForm = fb.group({
"sku": ["", Validators.compose([
Validators.required,
skuValidator
])]
});
Use Validators.compose([...]) to accpet mutli valiators.
In HTML:
<div *ng-if="sku.control.hasError('invalidSku')">
SKU is required
</div>
Code:
import {Component, View, FORM_DIRECTIVES, Validators, FormBuilder, NgIf} from 'angular2/angular2'; @Component({
selector: 'demo-form-sku'
})
@View({
directives: [FORM_DIRECTIVES, NgIf],
template: `
<div>
<h2>Demo Form: Sku</h2>
<!-- ngForm is attched to the form, and #f="form" form is also come from ngForm-->
<form [ng-form-model]="myForm"
(submit)="onSubmit(myForm.value)">
<div class="form-group" [class.has-error]="!sku.valid && sku.touched">
<label for="skuInput">SKU</label>
<input type="text"
class="form-control"
id="skuInput"
#sku = "form"
placeholder="SKU"
[ng-form-control]="myForm.controls['sku']">
</div>
<div *ng-if="!sku.control.valid"
class="bg-warning">SKU is invalid</div>
<button type="submit" class="btn btn-default">Submit
</button>
<div *ng-if="sku.control.hasError('invalidSku')">
SKU is required
</div> </form>
<div *ng-if="!myForm.valid"
class="bg-warning">Form is invalid</div>
</div>
`
}) export class DemoFormSku {
myForm: ControlGroup;
constructor(fb:FormBuilder) {
this.myForm = fb.group({
"sku": ["", Validators.compose([
Validators.required,
skuValidator
])]
});
this.sku = this.myForm.controls['sku'];
} onSubmit(value){
console.log(value);
} function skuValidator(control){
if(!control.value.match(/^123/)){
return {invalidSku: true};
}
}
}
[Angular 2] Custom Validtors的更多相关文章
- [Angular] Http Custom Headers and RequestOptions
updatePassenger(passenger: Passenger): Observable<Passenger> { let headers = new Headers({ 'Co ...
- [Angular] Create custom validators for formControl and formGroup
Creating custom validators is easy, just create a class inject AbstractControl. Here is the form we ...
- [Angular] Read Custom HTTP Headers Sent by the Server in Angular
By default the response body doesn’t contain all the data that might be needed in your app. Your ser ...
- [Angular 8] Custom Route Preloading with ngx-quicklink and Angular
In a previous lesson we learned about implementing a custom preloading strategy. That gives you a lo ...
- 来自 Thoughtram 的 Angular 2 系列资料
Angular 2 已经正式 Release 了,Thoughtram 已经发布了一系列的文档,对 Angular 2 的各个方面进行深入的阐释和说明. 我计划逐渐将这个系列翻译出来,以便对大家学习 ...
- Angular vs React---React-ing to change
这篇文章的全局观和思路一级棒! The Fairy Tale Cast your mind back to 2010 when users started to demand interactive ...
- Ionic + AngularJS
Ionic Framework Ionic framework is the youngest in our top 5 stack, as the alpha was released in lat ...
- angular custom Element 自定义web component
angular 自定义web组件: 首先创建一个名为myCustom的组件. 引入app.module: ... import {customComponent} from ' ./myCustom. ...
- [Angular] Angular Custom Change Detection with ChangeDetectorRef
Each component has its own ChangeDetectorRef, and we can inject ChangeDetectorRef into constructor: ...
随机推荐
- 『重构--改善既有代码的设计』读书笔记----Substitute Algorithm
重构可以把复杂的东西分解成一个个简单的小块.但有时候,你必须壮士断腕删掉整个算法,用简单的算法来取代,如果你发现做一件事情可以有更清晰的方式,那你完全有理由用更清晰的方式来解决问题.如果你开始使用程序 ...
- c++ string用法
首先,为了在我们的程序中使用string类型,我们必须包含头文件 .如下: #include //注意这里不是string.h string.h是C字符串头文件 1.声明一个C++字符串 声明一个字 ...
- vijos P1055奶牛浴场&& Winter Camp2002
这道题是我在寒假的模拟赛里碰到的,现在想起来仍觉得余味无穷.题目大意大致如下:给你一个矩形并在其中划出一个最大的子矩形,当然,在这个矩形里有些地方是取不到的,也就是说我们划的这个子矩形不能包含这些点( ...
- ecshop 嵌入地图加载不了问题
在ecshop 添加一个标识商家地理位置信息. 百度地图,加载不出来,查了下发现跟 js/transport.js 与 js/utils.js 两个文件有关在需要插入地图的地方去掉这两个文件的引用 地 ...
- eclipse4.2 UI换回 3.6版本的UI
Apparently, the Eclipse developers were kind enough to leave us an easy way out: From the Window men ...
- CentOS 6.3 卷组挂载硬盘教程 linux的VPS如何分区
XEN架构VPS提供的容量一般都不会低于10G,但大部分基于Xensystem面板的VPS默认挂载10G硬盘(第一磁盘),剩下的容量(第二磁盘)就需要通过手动挂载才能扩充默认的10G容量了.默认装完系 ...
- codevs 4163 hzwer与逆序对
传送门 题目描述 Description hzwer在研究逆序对. 对于数列{a},如果有序数对(I,j)满足:i<j,a[i]>a[j],则(i,j)是一对逆序对. 给定一个数列{a}, ...
- unity3d中脚本生命周期(MonoBehaviour lifecycle)
最近在做一个小示例,发现类继承于MonoBehaviour的类,有很多个方法,于是乎必然要问出一个问题:这么多个方法,执行先后顺序是如何的呢?内部是如何进行管理的呢?于是在网上找了许多资料,发现了Ri ...
- 查看SharePoint list的xml
http://{0}/_vti_bin/owssvr.dll?Cmd=Display&List={1}&XMLDATA=TRUE {0} – The URL to your site. ...
- All in All
Crawling in process... Crawling failed Description You have devised a new encryption technique which ...