[Angular 2] Validation

Define a filed should has validation:

export class DemoFormSku {
    myForm: ControlGroup;
    sku: AbstractControl;
    constructor(fb:FormBuilder) {
        this.myForm = fb.group({
            "sku": ["", Validators.required]
        });
        this.sku = this.myForm.controls[‘sku‘];
    }

    onSubmit(value){
        console.log(value);
    }
}

Form message

<div *ng-if="!myForm.valid"
              class="bg-warning">Form is invalid</div>

Field message

<div *ng-if="!sku.valid"
               class="bg-warning">SKU is invalid</div>

Field coloring

<div class="form-group" [class.has-error]="!sku.valid && sku.touched">
                <label for="skuInput">SKU</label>
                <input type="text"
                class="form-control"
                id="skuInput"
                placeholder="SKU"
                [ng-form-control]="myForm.controls[‘sku‘]">
            </div>

Specific validation

<div *ng-if="myForm.hasError(‘required‘, ‘sku‘)">
                SKU is required
            </div>
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"
                placeholder="SKU"
                [ng-form-control]="myForm.controls[‘sku‘]">
            </div>
            <div *ng-if="!sku.valid"
               class="bg-warning">SKU is invalid</div>
            <button type="submit" class="btn btn-default">Submit
            </button>
            <div *ng-if="myForm.hasError(‘required‘, ‘sku‘)">
                SKU is required
            </div>

        </form>
        <div *ng-if="!myForm.valid"
              class="bg-warning">Form is invalid</div>
       </div>
    `
})

export class DemoFormSku {
    myForm: ControlGroup;
    sku: AbstractControl;
    constructor(fb:FormBuilder) {
        this.myForm = fb.group({
            "sku": ["", Validators.required]
        });
        this.sku = this.myForm.controls[‘sku‘];
    }

    onSubmit(value){
        console.log(value);
    }
}
时间: 2024-12-21 03:42:30

[Angular 2] Validation的相关文章

ngVerify - 更高效的 angular 表单验证

ngVerify v1.5.0 a easy Angular Form Validation plugin.简洁高效的__angular表单验证插件__ See how powerful it.看看它有多强大 动态校验 自动关联提交按钮 多种 tip 校验消息提示 不只校验 dom 元素值,还可以校验 ngModel 数据模型 支持任意类型表单元素,甚至可以校验非表单元素 提供 type 类型校验模板,你几乎不需要定校验规则 提供自定义规则 支持第三方组件校验 Show HOME - 首页 DE

Angular Validation – Forms Validation with AngularJS (转)

Angular Validation – Forms Validation with AngularJS 31st March 2015   232   Angular-js Plugins , Form Elements Forms Validation with Angular made easy! Angular-Validation is an angular directive/service with locales (languages) with a very simple ap

[Angular2 Form] Display Validation and Error Messaging in Angular 2

Angular 2’s ngModel provides error objects for each of the built-in input validators. You can access these errors from a reference to the ngModel itself then build useful messaging around them to display to your users. First, you can use 'ngModel' fr

基于angular+bower+glup的webapp

一:bower介绍 1:全局安装安装bower cnpm i -g bower bower常用指令: bower init //初始化文件 bower install bower uninstall 2:bower初始化配置: bower init 后续的填写全部选yes 3:安装依赖(angularjs) bower install -- save angular 4:创建.bowerrc null>.bowerrc 这时候会提示null不是内部指令,但是没问题,已经创建好了. 然后在文件中输

[Angular] Implementing a ControlValueAccessor

So when you need to create a ControlValueAccessor? When you want to use a custom component as form control. For example a counter component, you can custom the style and the way to control the value changes. It needs some setup for control value acce

验证控件jQuery Validation Engine调用外部函数验证

在使用jQuery Validation Engine的时候,我们除了使用自带的API之外,还可以自己自定义正则验证.自定义正则验证上一篇已经讲过了,如果想使用自定义函数进行验证怎么办?其实这个控件有个bug,在api中说 也就是在我们需要进行验证的地方加上funcCall[自定义函数名],但是我们会发现总会报错,说找不到你这个函数名.其实它的要求是要添加required进行综合验证,也就是validate[required,funcCall[yorn]],这样它才识别我们的自定义函数.但是我们

[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

全栈老司机roadmap笔记--------(3)angular js

level 3 Forms and Models 如何添加和显示review? review内容作为product的内容的一部分!放在app.js文件里面 在html显示页面里面,增加一个循环来显示review的内容 我们如何把表单要填写的内容和我们要显示的内容进行绑定呢? 通过ng-model directive!!! 另外2个two-way binding example, radio button和check box 练习code 1 <!DOCTYPE html> 2 <html

[Angular2 Form] Create Radio Buttons for Angular 2 Forms

Using Radio Buttons in Angular 2 requires a basic understanding of forms as well as how their labels will match up with each input. This lesson shows how to use *ngFor with radio buttons and covers the quirks of the id property and forattributes as w