Angular material:尽管有正确的方法,但 mat-error 没有显示
Angular material: mat-error not showing despite true methods
我有一个要验证的确认密码 formcontrol。
当密码与确认密码输入中的值不同时,我想显示我的 mat-error 元素。为此,我有一个名为 equalPasswords()
的函数。如果功能相同,则我们收到 true,如果不同,我们收到 false。
<mat-form-field>
<input matInput placeholder="Repeat password" [formControl]="password2" type="password">
<mat-error *ngIf="password2.invalid && password2.hasError('required')">Password is required</mat-error>
<mat-error *ngIf="!equalPasswords() && !password2.hasError('required')">Passwords need to match</mat-error>
</mat-form-field>
我检查了控制台,当我在密码框中输入两个不同的输入时,它 equalPasswords() returns 错误。但是它仍然没有在 DOM.
中显示错误
有人知道如何解决这个问题吗?
Signup.component.ts
@Component({
selector: 'app-sign-up',
templateUrl: 'sign-up.component.html',
styleUrls: ['sign-up.component.css']
})
export class SignUpComponent implements OnInit {
mySignupForm: FormGroup;
countries = countries;
uniqueUsernameMessage;
uniqueEmailMessage;
formSubmitted = false;
@Output() closeSubmenu = new EventEmitter();
@ViewChild('select') select;
constructor(
private authService: AuthenticationService,
private route: Router,
private navigationService: NavigationService){}
get firstName() { return this.mySignupForm.get('firstName'); }
get lastName() { return this.mySignupForm.get('lastName'); }
get username() { return this.mySignupForm.get('username'); }
get email() { return this.mySignupForm.get('email'); }
get password1() { return this.mySignupForm.get('password1'); }
get password2() { return this.mySignupForm.get('password2'); }
get birthDate() { return this.mySignupForm.get('birthDate'); }
get country() { return this.mySignupForm.get('country'); }
get house() { return this.mySignupForm.get('house'); }
ngOnInit() {
this.mySignupForm = new FormGroup({
firstName: new FormControl(null, Validators.required),
lastName: new FormControl(null, Validators.required),
username: new FormControl(null, [Validators.required, Validators.minLength(5), Validators.maxLength(15)]),
birthDate: new FormControl(null, Validators.required),
password1: new FormControl(null, [Validators.required, Validators.minLength(6), Validators.maxLength(15), Validators.pattern('^.*(?=.{4,10})(?=.*\d)(?=.*[a-zA-Z]).*$')]),
password2: new FormControl(null, Validators.required),
email: new FormControl(null, [Validators.required, Validators.email]),
country: new FormControl(null, Validators.required),
house: new FormControl(null, Validators.required)
})
}
equalPasswords() {
console.log('equaltest', this.password1.value === this.password2.value);
return this.password1.value === this.password2.value;
}
}
MatFormField
只在FormControl
出错时显示mat-error
个元素。它不会仅仅因为您通过 ngIfElse
告诉它显示 - 这只适用于表单控件状态。解决此问题的一种方法是创建自定义验证器并使用它来检查密码是否匹配。您还可以在 equalPasswords()
函数的字段上设置错误。那应该是这样的:
equalPasswords(): boolean {
const matched: boolean = this.password1.value === this.password2.value;
console.log('equaltest', matched);
if (matched) {
this.signupForm.controls.password2.setErrors(null);
} else {
this.signupForm.controls.password2.setErrors({
notMatched: true
});
}
return matched;
}
我发现了一种可能会发生这种情况的非常微妙的情况。
如果你把 [formGroup]
指令 放在 <div>
之类的东西上,那么在提交表单时它不会得到 submitted == true
(它必须在 <form>
上才能发生)。
正在提交的表单是显示错误的两个触发器之一 - 另一个是触及该字段。而 touched 的意思是 onBlur
这意味着焦点一定是 lost.
所以可以得到这种情况:
- 您在 div
上有 [formGroup]
- 你有类似用户名/密码的表格。
- 您还有一个提交按钮。
- 您输入两个字段并按回车键。
- 您的验证触发错误但未显示!为什么!
- 因为您从未
blur
编辑密码字段(例如,您的光标仍在该框中)
- 最简单的解决方案是只将
[formGroup]
放在 <form>
标签上
- 另一种是创建自定义
ErrorStateMatcher
如果您将 formGroup
传递给 'dumb' 子组件(没有自己的形式),您必须使用依赖注入并传入 FormGroupDirective
而不是构造函数。
我有一个要验证的确认密码 formcontrol。
当密码与确认密码输入中的值不同时,我想显示我的 mat-error 元素。为此,我有一个名为 equalPasswords()
的函数。如果功能相同,则我们收到 true,如果不同,我们收到 false。
<mat-form-field>
<input matInput placeholder="Repeat password" [formControl]="password2" type="password">
<mat-error *ngIf="password2.invalid && password2.hasError('required')">Password is required</mat-error>
<mat-error *ngIf="!equalPasswords() && !password2.hasError('required')">Passwords need to match</mat-error>
</mat-form-field>
我检查了控制台,当我在密码框中输入两个不同的输入时,它 equalPasswords() returns 错误。但是它仍然没有在 DOM.
中显示错误有人知道如何解决这个问题吗?
Signup.component.ts
@Component({
selector: 'app-sign-up',
templateUrl: 'sign-up.component.html',
styleUrls: ['sign-up.component.css']
})
export class SignUpComponent implements OnInit {
mySignupForm: FormGroup;
countries = countries;
uniqueUsernameMessage;
uniqueEmailMessage;
formSubmitted = false;
@Output() closeSubmenu = new EventEmitter();
@ViewChild('select') select;
constructor(
private authService: AuthenticationService,
private route: Router,
private navigationService: NavigationService){}
get firstName() { return this.mySignupForm.get('firstName'); }
get lastName() { return this.mySignupForm.get('lastName'); }
get username() { return this.mySignupForm.get('username'); }
get email() { return this.mySignupForm.get('email'); }
get password1() { return this.mySignupForm.get('password1'); }
get password2() { return this.mySignupForm.get('password2'); }
get birthDate() { return this.mySignupForm.get('birthDate'); }
get country() { return this.mySignupForm.get('country'); }
get house() { return this.mySignupForm.get('house'); }
ngOnInit() {
this.mySignupForm = new FormGroup({
firstName: new FormControl(null, Validators.required),
lastName: new FormControl(null, Validators.required),
username: new FormControl(null, [Validators.required, Validators.minLength(5), Validators.maxLength(15)]),
birthDate: new FormControl(null, Validators.required),
password1: new FormControl(null, [Validators.required, Validators.minLength(6), Validators.maxLength(15), Validators.pattern('^.*(?=.{4,10})(?=.*\d)(?=.*[a-zA-Z]).*$')]),
password2: new FormControl(null, Validators.required),
email: new FormControl(null, [Validators.required, Validators.email]),
country: new FormControl(null, Validators.required),
house: new FormControl(null, Validators.required)
})
}
equalPasswords() {
console.log('equaltest', this.password1.value === this.password2.value);
return this.password1.value === this.password2.value;
}
}
MatFormField
只在FormControl
出错时显示mat-error
个元素。它不会仅仅因为您通过 ngIfElse
告诉它显示 - 这只适用于表单控件状态。解决此问题的一种方法是创建自定义验证器并使用它来检查密码是否匹配。您还可以在 equalPasswords()
函数的字段上设置错误。那应该是这样的:
equalPasswords(): boolean {
const matched: boolean = this.password1.value === this.password2.value;
console.log('equaltest', matched);
if (matched) {
this.signupForm.controls.password2.setErrors(null);
} else {
this.signupForm.controls.password2.setErrors({
notMatched: true
});
}
return matched;
}
我发现了一种可能会发生这种情况的非常微妙的情况。
如果你把 [formGroup]
指令 放在 <div>
之类的东西上,那么在提交表单时它不会得到 submitted == true
(它必须在 <form>
上才能发生)。
正在提交的表单是显示错误的两个触发器之一 - 另一个是触及该字段。而 touched 的意思是 onBlur
这意味着焦点一定是 lost.
所以可以得到这种情况:
- 您在 div 上有
- 你有类似用户名/密码的表格。
- 您还有一个提交按钮。
- 您输入两个字段并按回车键。
- 您的验证触发错误但未显示!为什么!
- 因为您从未
blur
编辑密码字段(例如,您的光标仍在该框中) - 最简单的解决方案是只将
[formGroup]
放在<form>
标签上 - 另一种是创建自定义
ErrorStateMatcher
[formGroup]
如果您将 formGroup
传递给 'dumb' 子组件(没有自己的形式),您必须使用依赖注入并传入 FormGroupDirective
而不是构造函数。