[Angular 2] Validation

Source: Internet
Author: User
Tags export class

Define a filed should has validation:

Export class Demoformsku {    myform:controlgroup;    Sku:abstractcontrol;    Constructor (Fb:formbuilder) {        this. MyForm = Fb.group ({            "SKU": ["" ", Validators.required]        });          This this. myform.controls[' SKU '];    }    OnSubmit (value) {        console.log (value);    }}

Form message

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

Field message

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

Field Coloring

<Divclass= "Form-group"[Class.has-error]= "!sku.valid && sku.touched">                <label for= "Skuinput">Sku</label>                <inputtype= "text"class= "Form-control"ID= "Skuinput"placeholder= "SKU"[Ng-form-control]= "myform.controls[' sku ')">            </Div>

Specific validation

<*ng-if= "myform.haserror (' Required ', ' SKU ')">                SKU is Required            </div>

Import {Component, View, Form_directives, validators, Formbuilder, ngif} from ' Angular2/angular2 '; @Component ({sel Ector: ' 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 was also come from Ngform -        <form[Ng-form-model]= "MyForm"(Submit)= "OnSubmit (myform.value)">            <Divclass= "Form-group"[Class.has-error]= "!sku.valid && sku.touched">                <label for= "Skuinput">Sku</label>                <inputtype= "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>            <Buttontype= "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); }}

[Angular 2] Validation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.