gpt4 book ai didi

angular - 在 Angular 4 Reactive Forms 中提交时显示验证消息

转载 作者:太空狗 更新时间:2023-10-29 17:42:29 28 4
gpt4 key购买 nike

我正在使用 Angular 4,响应式表单。我想在用户点击提交/创建帐户按钮时显示验证错误消息。这是我正在使用的 HTML 和 typescript 代码。

<form [formGroup]="signupForm" (ngSubmit)="onSubmit()">

<div class="form-group">
<input type="text"
id="firstname"
name="firstname"
formControlName="firstname"
placeholder="First Name">
<span *ngIf="!signupForm.get('firstname').valid && signupForm.get('firstname').touched"
class="help-block"> Please Enter First Name (Minimum 2 Characters)</span>
</div>

<div class="form-group">
<input type="text"
id="lastname"
name="lastname"
formControlName="lastname"
placeholder="Last Name">
<span *ngIf="!signupForm.get('lastname').valid && signupForm.get('lastname').touched"
class="help-block"> Please Enter Last Name (Minimum 2 Characters)</span>
</div>

<div class="form-group">
<button type="submit"
class="btn btn-success btn-lg btn-qte">Create Account</button>
</div>

</form>

类型脚本代码


export class UserRegistrationComponent implements OnInit {
signupForm: FormGroup;

ngOnInit() {
this.signupForm = new FormGroup({
'firstname': new FormControl(null, [Validators.required, Validators.minLength(2)]),
'lastname': new FormControl(null, [Validators.required, Validators.minLength(2),]),
'businessname': new FormControl(null),
'phonenumber': new FormControl(null, [Validators.required]),
'email': new FormControl(null, [Validators.required, Validators.email]),
'password': new FormControl(null, [Validators.required]),
'confirmpassword': new FormControl(null, Validators.required)

});
}

onSubmit() {
console.log(this.signupForm)
}

}

最佳答案

有点像

onSubmit() {
console.log(this.signupForm)
this.signupForm.controls['firstname'].markAsTouched()
}

关于angular - 在 Angular 4 Reactive Forms 中提交时显示验证消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44774192/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com