gpt4 book ai didi

angular - 错误 : Expected validator to return Promise or Observable

转载 作者:行者123 更新时间:2023-12-02 16:54:36 28 4
gpt4 key购买 nike

我在 ReactiveFormsModule 中创建函数 muchmatch ,但是在测试时,发现 Angular 中有一个错误消息,我真的不知道为什么。

“错误:期望验证器返回 Promise 或 Observable。”

import { Component, OnInit } from '@angular/core';
import { UserService } from '../service/user.service';
import { Router} from '@angular/router';

import { FormBuilder, FormGroup, Validators } from '@angular/forms';

@Component({
selector: 'app-login-register',
templateUrl: './login-register.component.html',
styleUrls: ['./login-register.component.css']
})
export class LoginRegisterComponent implements OnInit {

registerForm: FormGroup;
submitted = false;

constructor(private formBuilder: FormBuilder) { }

ngOnInit() {
this.registerForm = this.formBuilder.group({
name: ['', Validators.required],
lastName: ['', Validators.required],
email: ['', [Validators.required, Validators.email]],
password: ['', [Validators.required, Validators.minLength(6)]],
confirmPassword: ['', Validators.required, this.mustMatch('password', 'confirmPassword')],
passwordHint: ['', [Validators.required, Validators.minLength(6)]],
confirmPasswordHint: ['', Validators.required , this.mustMatch('passwordHint', 'confirmPasswordHint')],
});
}

// convenience getter for easy access to form fields
get f() { return this.registerForm.controls; }

//提交时

  onSubmit() {

this.submitted = true;

// stop here if form is invalid
if (this.registerForm.invalid) {
return;
}

alert('SUCCESS!! :-)\n\n' + JSON.stringify(this.registerForm.value));
}

//功能查询密码

  mustMatch(p1 , p2) {

return (registerForm: FormGroup) => {

let pass1 = this.registerForm.controls[p1];
let pass2 = this.registerForm.controls[p2];

// if (pass1.errors && !pass2.errors.mustMatch) {
// return;
// }

if (pass1.value !== pass2.value) {
pass2.setErrors({mustMatch: true});
} else {
pass2.setErrors(null);
}
}
}
}

//Code Show form register Data Resive to backend error when Fill in the column passwordconfirm, passwordHintconfrim信息

<form [formGroup]="registerForm" (ngSubmit)="onSubmit()" style="width: 500px">

<div class="form-group">
<label>First Name</label>
<input type="text" formControlName="name" class="form-control" [ngClass]="{ 'is-invalid': submitted && f.name.errors }"
placeholder="input your name">
<div *ngIf="submitted && f.name.errors" class="invalid-feedback">
<div *ngIf="f.name.errors.required">First Name is required</div>
</div>
</div>

<div class="form-group">
<label>Last Name</label>
<input type="text" formControlName="lastName" class="form-control" [ngClass]="{ 'is-invalid': submitted && f.lastName.errors }"
placeholder="input your name">
<div *ngIf="submitted && f.lastName.errors" class="invalid-feedback">
<div *ngIf="f.lastName.errors.required">Last Name is required</div>
</div>
</div>

<div class="form-group">
<label>Email</label>
<input type="text" formControlName="email" class="form-control" [ngClass]="{ 'is-invalid': submitted && f.email.errors }"
placeholder="input your name">
<div *ngIf="submitted && f.email.errors" class="invalid-feedback">
<div *ngIf="f.email.errors.required">Email is required</div>
<div *ngIf="f.email.errors.email">Email must be a valid email address</div>
</div>
</div>
<!--Password-->
<div class="form-group">
<label>Password</label>
<input type="password" formControlName="password" class="form-control" [ngClass]="{ 'is-invalid': submitted && f.password.errors }"
placeholder="input your name">
<div *ngIf="submitted && f.password.errors" class="invalid-feedback">
<div *ngIf="f.password.errors.required">Password is required</div>
<div *ngIf="f.password.errors.minlength">Password must be at least 6 characters</div>
</div>
</div>
<div class="form-group">
<label>Confirm Password</label>
<input type="password" formControlName="confirmPassword" class="form-control" [ngClass]="{ 'is-invalid': submitted && f.confirmPassword.errors }"
placeholder="input your name">
<div *ngIf="submitted && f.confirmPassword.errors" class="invalid-feedback">
<div *ngIf="f.confirmPassword.errors.required">Confirm Password is required</div>
<div *ngIf="f.confirmPassword.errors.mustMatch">Passwords must match</div>
</div>
</div>
<!--Password Hint-->
<div class="form-group">
<label>PasswordHint</label>
<input type="password" formControlName="passwordHint" class="form-control" [ngClass]="{ 'is-invalid': submitted && f.passwordHint.errors }"
placeholder="input your name">
<div *ngIf="submitted && f.passwordHint.errors" class="invalid-feedback">
<div *ngIf="f.passwordHint.errors.required">Password is required</div>
<div *ngIf="f.passwordHint.errors.minlength">Password must be at least 6 characters</div>
</div>
</div>
<div class="form-group">
<label>Confirm PasswordHint</label>
<input type="password" formControlName="confirmPasswordHint" class="form-control" [ngClass]="{ 'is-invalid': submitted && f.confirmPasswordHint.errors }"
placeholder="input your name">
<div *ngIf="submitted && f.confirmPasswordHint.errors" class="invalid-feedback">
<div *ngIf="f.confirmPasswordHint.errors.required">Confirm Password is required</div>
<div *ngIf="f.confirmPasswordHint.errors.mustMatch">Passwords must match</div>
</div>
</div>

<div class="form-group">
<button class="btn btn-primary">Register</button>
</div>
</form>

最佳答案

在使用 FormBuilder 时,您应该知道最常用的语法如下:

this.fb.group({
property: [value, syncValidator|s, asyncValidator|s]
})

现在让我们看看你写了什么:

confirmPasswordHint: ['', Validators.required , this.mustMatch(...)]
\/ \/
syncValidator asyncValidator

但你可能想要:

confirmPasswordHint: ['', [Validators.required , this.mustMatch(...)]]
\/
Array of syncValidators

FormBuilder 备忘单

它可以带哪些参数:

enter image description here

用例:

enter image description here

关于angular - 错误 : Expected validator to return Promise or Observable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56999654/

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