- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我创建了一个带有验证检查的表单,
import { FormGroup, Validators, FormControl } from "@angular/forms";
目前我有提交按钮
[disabled]
直到表单被正确填写。我向用户显示表单是否有效的唯一方法是在表单字段无效时在输入中显示红色。
我想向用户显示一条错误消息,告诉他们哪里出了问题。
Angular2 中是否内置了一些东西来显示错误消息或为什么输入字段无效?或者我是否需要为每个表单字段构建自定义错误消息?如果是这样,我将如何检查每个输入字段,比方说当有人离开各自字段的焦点时。因此,如果他们离开输入字段并且它无效,那么我可以显示一条消息告诉他们它无效以及为什么?
我有一种显示消息的方法,我只需要想出一种获取错误消息的方法。换句话说,从表单中生成有关其无效原因的文本。
示例
Please provide a valid mobile number
代码
正则表达式
ngOnInit() {
this.personalForm = new FormGroup({
email : new FormControl(this.auth.user.email,Validators.required ),
first_name: new FormControl(null,[
Validators.required,
Validators.pattern("^[a-zA-Zñáéíóúü']{1,30}$")
]),
middle_name: new FormControl(null,[
Validators.required,
Validators.pattern("^[a-zA-Zñáéíóúü']{1,30}$")
]),
last_name: new FormControl(null,[
Validators.required,
Validators.pattern("^[a-zA-Zñáéíóúü']{1,30}$")
]),
dob : new FormControl (null, [
Validators.required,
Validators.pattern("[1][9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]|[2][0][0-9][0-9]-[0-9][0-9]-[0-9][0-9]")
]),
mobile_phone: new FormControl(null, [
Validators.required,
Validators.pattern("[0-9][0-9][0-9]-[0-9][0-9][0-9]-[0-9][0-9][0-9][0-9]|[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]")
]),
home_phone: new FormControl(null, [
Validators.required,
Validators.pattern("[0-9][0-9][0-9]-[0-9][0-9][0-9]-[0-9][0-9][0-9][0-9]|[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]")
]),
business_phone: new FormControl(null, [
Validators.required,
Validators.pattern("[0-9][0-9][0-9]-[0-9][0-9][0-9]-[0-9][0-9][0-9][0-9]|[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]")
]),
fax_number: new FormControl(null, [
Validators.required,
Validators.pattern("[0-9][0-9][0-9]-[0-9][0-9][0-9]-[0-9][0-9][0-9][0-9]|[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]")
]),
ssn: new FormControl(null, [
Validators.required,
Validators.pattern("[0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9][0-9][0-9]|[0-9][0-9][0-9]/[0-9][0-9]/[0-9][0-9][0-9][0-9]|[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]")
])
});
}
有效的 bool 值/处理表单数据
save(model: Personal, isValid: boolean) {
if (!isValid) {
console.log('Personal Form is not valid');
console.log(model, isValid);
} else {
console.log('Personal Form is valid');
console.log(model, isValid);
let headers = new Headers();
headers.append('Content-Type', 'application/json');
return this.http
.post('http://localhost:4200/api/updateProfile',
model,
{headers: headers})
.map((res: Response) => res.json())
.subscribe((res) => {
//do something with the response here
console.log(res);
});
}
}
表单
<form [formGroup]="personalForm" novalidate (ngSubmit)="save(personalForm.value, personalForm.valid)">
<div class="row">
<div class="col-sm-12 col-md-12 col-lg-12">
<div class="card card-inverse card-success">
<div class="card-header">
<strong>Personal Information</strong>
</div>
<div class="card-block">
<div class="row">
<div class="form-group col-sm-12 col-md-12">
<label for="email">Email</label>
<div class="input-group">
<span class="input-group-addon"><i class="fa fa-envelope"></i>
</span>
<input type="text" class="form-control" id="email" formControlName="email" placeholder="{{personal.email}}"readonly>
</div>
<div class="alert alert-danger" *ngIf="!personalForm.controls.email.valid && submitted ">
*Email is required.
</div>
</div>
</div>
最佳答案
总结
要访问 FormGroup
input
字段,您可以使用 syntax
访问您创建的 controls
。
this.FormGroupName.get('ControlName').status
这将返回 VALID
或 INVALID
在我的例子中,这是如何完成的,
示例
导入正确的包,
import {FormGroup, Validators, FormControl } from "@angular/forms";
import {INVALID, VALID} from "@angular/forms/src/model";
创建FormGroup
,
personalForm: FormGroup;
创建FormControl
,
ngOnInit() {
this.personalForm = new FormGroup({
first_name: new FormControl(null,[
Validators.required,
Validators.pattern("^[a-zA-Zñáéíóúü']{1,30}$")
]),
});
}
将 FormControlName
添加到 form
和要检查 error
时调用的 function
.
<input (blur)="firstNameValidate('*First Name Required', 'Use 1-30 letters to spell your name.')" type="text" class="form-control" placeholder="Enter first name" id="first_name" formControlName="first_name">
检查 VALID
或 INVALID
,
firstNameValidate(ErrorTitle, ErrorMessage) {
if (this.personalForm.get('first_name').status == VALID) {
this.getSuccess("First Name Entered", "First name entered correctly");
} else {
this.toastWarning(ErrorTitle, ErrorMessage);
}
}
调用错误
在我的例子中,我决定在 toast
中显示我的错误。所以我使用 blur
来跟踪用户何时离开 input
字段。当用户移出 input
字段时,将调用 firstNameValidate()
函数并根据值显示正确的 toast
,有效
或 无效
。根据 response
这两个 functions
之一被触发。
toastWarning(ErrorTitle, ErrorMessage) {
var toastOptions:ToastOptions = {
title: ErrorTitle,
msg: ErrorMessage,
showClose: true,
timeout: 7000,
theme: 'bootstrap',
onAdd: (toast:ToastData) => {
console.log('Toast ' + toast.id + ' has been added!');
},
onRemove: function(toast:ToastData) {
console.log('Toast ' + toast.id + ' has been removed!');
}
};
this.toastyService.warning(toastOptions);
}
getSuccess(SuccessTitle, SuccessMessage) {
var toastOptions:ToastOptions = {
title: SuccessTitle,
msg: SuccessMessage,
showClose: true,
timeout: 5000,
theme: 'bootstrap',
onAdd: (toast:ToastData) => {
console.log('Toast ' + toast.id + ' has been added!');
},
onRemove: function(toast:ToastData) {
console.log('Toast ' + toast.id + ' has been removed!');
}
};
this.toastyService.success(toastOptions);
}
然后用户看到正确的toast
/message
,
成功
警告
关于angular - 在用户填写表单 Angular 2 时检查表单验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40966603/
单向链表 单向链表比顺序结构的线性表最大的好处就是不用保证存放的位置,它只需要用指针去指向下一个元素就能搞定。 单链表图解 图画的比较粗糙,简单的讲解一下: 上面四个长方形,每个长方
使用TCP,我正在设计一些类似于next的程序。 客户端在许多线程中的接收正在等待一台服务器的发送消息。但是,这是有条件的。 recv正在等待特定的发送消息。 例如 客户 thread 1: recv
我正在编写正则表达式来验证电子邮件。唯一让我困惑的是: 顶级域名可以使用单个字符吗?(例如:lockevn.c) 背景:我知道顶级域名可以是 2 个字符到任意字符(.uk、.us 到 .canon、.
是否可以在单个定义中定义同一 Controller 的多个路由? 例如: 我想要一个单一的定义 /, /about, /privacy-policy 使用类似的东西 _home: pat
我正在使用 objective-c开发针对 11.4 iOS 的单 View 应用程序,以及 Xcode版本是 9.4.1。 创建后有Main.storyboard和LaunchScreen.stor
我一直在尝试在 shell 程序中实现管道结构,如果我执行简单的命令(例如“hello | rev”),它就可以工作 但是当我尝试执行“head -c 1000000/dev/urandom | wc
此表包含主机和接口(interface)列UNIQUE 组合* 编辑:这个表也有一个自动递增的唯一 ID,抱歉我应该在之前提到这个 ** | host.... | interface..... |
我想将具有固定补丁大小的“std filter”应用于单 channel 图像。 也就是说,我希望 out[i,j] 等于 img[i,j] 附近的像素值的标准值。 对于那些熟悉 Matlab 的人,
假设我想进行网络调用并使用 rx.Single,因为我希望只有一个值。 我如何应用replay().autoConnect() 这样的东西,这样当我从多个来源订阅时网络调用就不会发生多次?我应该使用
我将图像从 rgb 转换为 YUV。现在我想单独找到亮度 channel 的平均值。你能告诉我如何实现这一目标吗?此外,有没有办法确定图像由多少个 channel 组成? 最佳答案 你可以这样做: #
在比较Go和Scala的语句结束检测时,我发现Scala的规则更丰富,即: A line ending is treated as a semicolon unless one of the foll
在IEEE 1800-2005或更高版本中,&和&&二进制运算符有什么区别?它们相等吗? 我注意到,当a和b的类型为bit时,这些coverpoint定义的行为相同: cp: coverpoint a
我正在使用Flutter的provider软件包。我要实现的是为一个 View 或页面提供一个简单的提供程序。因此,我在小部件中尝试了以下操作: Widget build(BuildContext c
我正在尝试在 cython 中使用 openmp。我需要在 cython 中做两件事: i) 在我的 cython 代码中使用 #pragma omp single{} 作用域。 ii) 使用#pra
我正在尝试从转义字符字符串中删除单引号和双引号。它对单引号 ' 或双自动 " 不起作用。 请问有人可以帮忙吗? var mysting = escapedStr.replace(/^%22/g, '
我正在尝试在 cython 中使用 openmp。我需要在 cython 中做两件事: i) 在我的 cython 代码中使用 #pragma omp single{} 作用域。 ii) 使用#pra
我正在使用 ANT+ 协议(protocol),将智能手机与 ANT+ USB 加密狗连接,该加密狗通过 SimulANT+ 连接到 PC。 SimulANT+ 正在模拟一个心率传感器,它将数据发送到
有人可以解释/理解单/多线程模式下计算结果的不同吗? 这是一个大约的例子。圆周率的计算: #include #include #include const int itera(100000000
我编写了一个粗略的阴影映射实现,它使用 6 个不同的 View 矩阵渲染场景 6 次以创建立方体贴图。 作为优化,我正在尝试使用几何着色器升级到单 channel 方法,但很难从我的着色器获得任何输出
尝试使用 Single-Spa 构建一些东西并面临添加到应用程序 AngularJS 的问题。 Angular2 和 ReactJs 工作完美,但如果添加 AngularJS 并尝试为此应用程序使用
我是一名优秀的程序员,十分优秀!