[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
重构可以把复杂的东西分解成一个个简单的小块.但有时候,你必须壮士断腕删掉整个算法,用简单的算法来取代,如果你发现做一件事情可以有更清晰的方式,那你完全有理由用更清晰的方式来解决问题.如果你开始使用程序 ...
- Telerik_2012_Q3 破解全套下载链接
1.Telerik_OpenAccess_ORM_2012_3_1012_SDK.zip (暂未提供下载) 2. Telerik_OpenAccess_ORM_2012_3_1012.zip 3. T ...
- Bootstrap_表单_图标
在Bootstrap框架中是通过给元素添加“glyphicon”类名来实现,然后通过伪元素“:before”的“content”属性调取对应的icon编码: <span class=" ...
- jquery 自定义tab
<!DOCTYPE html> <html> <head> <script src="/jquery/jquery-1.11.1.min.js&qu ...
- php 钩子函数原理 解析
目前对钩子的理解:<转载:http://www.cnblogs.com/del/archive/2008/02/25/1080825.html> 譬如我们用鼠标在某个窗口上双击了一次, 或 ...
- nginx+uwsgi+WSGI applications
uwsgi一个专业的部署运用的工具,不仅能够部署Python运用,还能够部署其他运用比如Perl,Ruby等 uWSGI 安装: pip install uwsgi WSGI application( ...
- BZOJ 3893 Cow Jog
Description The cows are out exercising their hooves again! There are \(N\) cows jogging on an infin ...
- ubuntu删除openjdk,安装 Sun JDK
1.到官网下载安装包: jdk-7-linux-i586.tar.gz 2.创建安装目录:sudo mkdir /usr/lib/jvm 3. 解压缩:tar zxvf ./jdk-7-linux-i ...
- CentOS 5升级Python版本(2.4>2.7)
安装SALT时,需要这样作,公司有一批REDHAT5的,弄起来... 然后却是: Missing Dependency: python(abi) = 2.6 is needed by package ...
- 获得进程可执行文件的路径: GetModuleFileNameEx, GetProcessImageFileName, QueryFullProcessImageName
http://blog.csdn.net/bichenggui/article/details/4774457 -------------------------------------------- ...