typescript angular 5 验证表单控件更新需要
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/48292000/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
angular 5 validation form control update with required
提问by Charles Morrison
I am using Angular 5. And How do I add required validation to an existing form control??
我正在使用 Angular 5。我如何将所需的验证添加到现有的表单控件中?
this.formSearch.get('name').clearValidators();
this.formSearch.get('name').updateValueAndValidity();
回答by Lakindu Gunasekara
Angular comes with a small set of pre-built validators to match the ones we can define via standard HTML5 attributes
, namely required
, minlegth
, maxlength
and pattern
which we can access from the Validators module.
角带有一个小套预先建立验证程序以匹配我们可以通过标准的HTML5定义的那些attributes
,namely required
,minlegth
,maxlength
并且pattern
我们可以从校验模块访问。
The first parameter of a FormControl
constructor is the initial value of the control, we'll leave that as empty string. The second parameter contains either a single validator if we only want to apply one, or a list of validators if we want to apply multiple validators to a single control.
FormControl
构造函数的第一个参数是控件的初始值,我们将其保留为空字符串。如果我们只想应用一个验证器,第二个参数包含一个验证器,如果我们想将多个验证器应用到单个控件,则包含一个验证器列表。
import { FormGroup, FormControl, Validators } from '@angular/forms';
class ModelFormComponent implements OnInit {
myform: FormGroup;
ngOnInit() {
myform = new FormGroup({
name: new FormGroup({
firstName: new FormControl('', Validators.required),
lastName: new FormControl('', Validators.required),
}),
email: new FormControl('', [
Validators.required,
Validators.pattern("[^ @]*@[^ @]*")
]),
password: new FormControl('', [
Validators.minLength(8),
Validators.required
]),
language: new FormControl()
});
}
}
- We add a single required validator to mark this control as required.
- We can also provide an array of validators.
- We specify a pattern validator which checks whether the email contains a @ character.
- The minlength validator checks to see if the password is a minimum of 8 characters long.
- We don't add any validators to the language select box.
- 我们添加了一个必需的验证器来将此控件标记为必需的。
- 我们还可以提供一系列验证器。
- 我们指定了一个模式验证器,用于检查电子邮件是否包含 @ 字符。
- minlength 验证器检查密码是否至少为 8 个字符长。
- 我们不会向语言选择框添加任何验证器。
IN YOUR SCENARIOYou can use something like this
在你的场景中你可以使用这样的东西
this.formSearch.controls["name"].setValidators([Validators.required]??,[Validators.minLength(1), Validators.maxLength(30)]);
Here you simply pass the FormControl an array of validators. And this will reset any existing validators you added when you created the FormControl.
在这里,您只需向 FormControl 传递一个验证器数组。这将重置您在创建 FormControl 时添加的任何现有验证器。
Additionally,
此外,
Set a validator for a control in the FormGroup:
this.formSearch.controls['name'].setValidators([Validators.required])
Remove the validator from the control in the FormGroup:
this.formSearch.controls['name'].clearValidators()
Update the FormGroup once you have run either of the above lines.
this.formSearch.controls['name'].updateValueAndValidity()
为 FormGroup 中的控件设置验证器:
this.formSearch.controls['name'].setValidators([Validators.required])
从 FormGroup 中的控件中删除验证器:
this.formSearch.controls['name'].clearValidators()
运行上述任一行后,更新 FormGroup。
this.formSearch.controls['name'].updateValueAndValidity()