Validation

----primeng输入组件显示验证错误时自动标记为无效值。

demo code

export class ValidationDemo implements OnInit {

    msgs: Message[] = [];

    userform: ControlGroup;

    submitted: boolean;

    genders: SelectItem[];

    description: string;

    constructor(private fb: FormBuilder) {}

    ngOnInit() {
this.userform = this.fb.group({
'firstname': new Control('', Validators.required),
'lastname': new Control('', Validators.required),
'password': new Control('', Validators.compose([Validators.required, Validators.minLength(6)])),
'description': new Control(''),
'gender': new Control('', Validators.required)
}); this.genders = [];
this.genders.push({label:'Select Gender', value:''});
this.genders.push({label:'Male', value:'Male'});
this.genders.push({label:'Female', value:'Female'});
} onSubmit(value: string) {
this.submitted = true;
this.msgs = [];
this.msgs.push({severity:'info', summary:'Success', detail:'Form Submitted'});
} get diagnostic() { return JSON.stringify(this.userform.value); } }

ValidationDemo.ts

<p-growl [value]="msgs" sticky="sticky"></p-growl>

<form [formGroup]="userform" (ngSubmit)="onSubmit(userform.value)">
<p-panel header="Validate">
<div class="ui-grid ui-grid-responsive ui-grid-pad ui-fluid" style="margin: 10px 0px">
<div class="ui-grid-row">
<div class="ui-grid-col-2">
First Name *:
</div>
<div class="ui-grid-col-6">
<input pInputText type="text" formControlName="firstname" placeholder="Required"/>
</div>
<div class="ui-grid-col-4">
<div class="ui-message ui-messages-error ui-corner-all" *ngIf="!userform.controls['firstname'].valid&&userform.controls['firstname'].dirty">
<i class="fa fa-close"></i>
Firstname is required
</div>
</div>
</div>
<div class="ui-grid-row">
<div class="ui-grid-col-2">
Last Name *:
</div>
<div class="ui-grid-col-6">
<input pInputText type="text" formControlName="lastname" placeholder="Required"/>
</div>
<div class="ui-grid-col-4">
<div class="ui-message ui-messages-error ui-corner-all" *ngIf="!userform.controls['lastname'].valid&&userform.controls['lastname'].dirty">
<i class="fa fa-close"></i>
Lastname is required
</div>
</div>
</div>
<div class="ui-grid-row">
<div class="ui-grid-col-2">
Password *:
</div>
<div class="ui-grid-col-6">
<input pInputText type="password" formControlName="password" placeholder="Required - Min(6)"/>
</div>
<div class="ui-grid-col-4">
<div class="ui-message ui-messages-error ui-corner-all" *ngIf="!userform.controls['password'].valid&&userform.controls['password'].dirty">
<i class="fa fa-close"></i>
<span *ngIf="userform.controls['password'].errors['required']">Password is required</span>
<span *ngIf="userform.controls['password'].errors['minlength']">Must be longer than 6 characters</span>
</div>
</div>
</div>
<div class="ui-grid-row">
<div class="ui-grid-col-2">
Description:
</div>
<div class="ui-grid-col-6">
<textarea pInputTextarea type="text" formControlName="description"></textarea>
</div>
<div class="ui-grid-col-4"></div>
</div>
<div class="ui-grid-row">
<div class="ui-grid-col-2">
Gender *:
</div>
<div class="ui-grid-col-6">
<p-dropdown [options]="genders" formControlName="gender" [autoWidth]="false"></p-dropdown>
</div>
<div class="ui-grid-col-4">
<div class="ui-message ui-messages-error ui-corner-all" *ngIf="!userform.controls['gender'].valid&&userform.controls['gender'].dirty">
<i class="fa fa-close"></i>
Gender is required
</div>
</div>
</div>
<div class="ui-grid-row">
<div class="ui-grid-col-2"></div>
<div class="ui-grid-col-6">
<button pButton type="submit" label="Submit" [disabled]="!userform.valid"></button>
</div>
<div class="ui-grid-col-4"></div>
</div>
<div style="text-align:center;margin-top:20px" *ngIf="submitted">
Form Submitted
<br>
{{diagnostic}}
</div>
</div>
</p-panel>
</form>

ValidationDemo.html

PrimeNG之Validation的更多相关文章

  1. jQuery学习之路(8)- 表单验证插件-Validation

    ▓▓▓▓▓▓ 大致介绍 jQuery Validate 插件为表单提供了强大的验证功能,让客户端表单验证变得更简单,同时提供了大量的定制选项,满足应用程序各种需求.该插件捆绑了一套有用的验证方法,包括 ...

  2. jQuery- 表单验证插件-Validation

      ▓▓▓▓▓▓ 大致介绍 jQuery Validate 插件为表单提供了强大的验证功能,让客户端表单验证变得更简单,同时提供了大量的定制选项,满足应用程序各种需求.该插件捆绑了一套有用的验证方法, ...

  3. 交叉验证(Cross Validation)原理小结

    交叉验证是在机器学习建立模型和验证模型参数时常用的办法.交叉验证,顾名思义,就是重复的使用数据,把得到的样本数据进行切分,组合为不同的训练集和测试集,用训练集来训练模型,用测试集来评估模型预测的好坏. ...

  4. MVC学习系列13--验证系列之Remote Validation

    大多数的开发者,可能会遇到这样的情况:当我们在创建用户之前,有必要去检查是否数据库中已经存在相同名字的用户.换句话说就是,我们要确保程序中,只有一个唯一的用户名,不能有重复的.相信大多数人都有不同的解 ...

  5. MVC学习系列12---验证系列之Fluent Validation

    前面两篇文章学习到了,服务端验证,和客户端的验证,但大家有没有发现,这两种验证各自都有弊端,服务器端的验证,验证的逻辑和代码的逻辑混合在一起了,如果代码量很大的话,以后维护扩展起来,就不是很方便.而客 ...

  6. jQuery Validation Engine 表单验证

    功能强大的 jQuery 表单验证插件,适用于日常的 E-mail.电话号码.网址等验证及 Ajax 验证,除自身拥有丰富的验证规则外,还可以添加自定义的验证规则. 兼容 IE 6+, Chrome, ...

  7. 架构验证过程发现非数据类型错误 validation found non-data type errors

    问题: infopath报一下错误 validation found non-data type errors 架构验证过程发现非数据类型错误 原因: 重复表字段在后台代码里要一一对应,否则报错. 错 ...

  8. ORA-19563: header validation failed for file

    在测试服务器还原数据库时遇到了ORA-19563错误.如下所示 RMAN-00571: ======================================================== ...

  9. [WPF系列]-Data Validation

    项目经常前台界面涉及到用户输入时,我们常常会用到数据有效性的验证.在网页中我们之前用js来校验Form中的数据有效性.在WPF中我们如何实现这种验证机制了?答案:INotifyDataErrorInf ...

随机推荐

  1. 30天自制操作系统 - 来一个hello world

    helloos.nas 源码: ; hello-os ; TAB= ; 以下这段是标准的FAT12格式软盘专用代码 DB 0xeb, 0x4e, 0x90 DB "HELLOIPL" ...

  2. CSS padding

    CSS padding 是用来控制div table 内间距的,下面我们就来讲一下padding 实例吧.   CSS padding 利用CSS填充,你将能够更改默认的间隙内出现的各种HTML元素( ...

  3. Mathmatica简介

    作者:桂. 时间:2018-06-27  21:53:34 链接:https://www.cnblogs.com/xingshansi/p/9236502.html 前言 打算系统学习一些数学知识,容 ...

  4. Atitit sleep原理 node.js sleep解决方案 timer

    Atitit  sleep原理  node.js sleep解决方案  timer sleep()的实现分为三步: 1.注册一个信号signal(SIGALRM,handler).接收内核给出的一个信 ...

  5. 用virsh console vhosts 卡住

    [root@666 ok]# virsh list --all Id Name State ---------------------------------------------------- 1 ...

  6. 【资料下载区】【iCore1S相关代码、资料下载地址】更新日期2017/10/09

    [iCore1S相关文档][更新中...] iCore1S原理图(PDF)下载iCore1S引脚注释(PDF)下载 [iCore1S相关例程代码][ARM][更新中...] DEMO1.0测试程序发布 ...

  7. WPF Button 样式

    WPF CheckBox 自定义样式 给Button设置ToolTip <Style TargetType="{x:Type Button}" x:Key="Def ...

  8. laravel5.8笔记六:公共函数和常量设置

    公共函数 创建bootstrap/common.php <?php // 发送短信 function sendSMS($mobile){ } // 发送邮件 function sendMail( ...

  9. Mysql Window 解压版卸载

    windows如何彻底卸载mysql 如何彻底删除mysql 1.首先在windows服务中将mysql服务删掉,使用命令 sc delete mysql 2.在控制面板中卸载掉mysql. 3.清理 ...

  10. iOS开发之--在UIWindow上展示/移除一个View

    代码如下: 展示 UIWindow *window = [[UIApplication sharedApplication].windows lastObject]; [window addSubvi ...