gpt4 book ai didi

angular - 我想用 react 形式和 Angular Material 一次显示一个错误

转载 作者:行者123 更新时间:2023-12-02 03:31:14 24 4
gpt4 key购买 nike

我正在使用 Angular react 形式和 Angular Material 。我的代码运行良好。但是我想一次显示一个错误

我的 .html 文件代码

      <form [formGroup]="accountDetailsForm" novalidate (ngSubmit)="onSubmitAccountDetails(accountDetailsForm.value)">

<mat-form-field class="full-width">
<input matInput maxlength="25" placeholder="Username" formControlName="username" required>
<mat-error *ngFor="let validation of account_validation_messages.username">
<mat-error class="error-message" *ngIf="accountDetailsForm.get('username').hasError(validation.type) && (accountDetailsForm.get('username').dirty || accountDetailsForm.get('username').touched)">{{validation.message}}</mat-error>
</mat-error>
</mat-form-field>

<mat-form-field class="full-width">
<input matInput type="email" placeholder="Email" formControlName="email" required>
<mat-error *ngFor="let validation of account_validation_messages.email">
<mat-error class="error-message" *ngIf="accountDetailsForm.get('email').hasError(validation.type) && (accountDetailsForm.get('email').dirty || accountDetailsForm.get('email').touched)">{{validation.message}}</mat-error>
</mat-error>
</mat-form-field>


<div formGroupName="matching_passwords">
<mat-form-field class="full-width">
<input matInput type="password" placeholder="Password" formControlName="password" required>
<mat-error *ngFor="let validation of account_validation_messages.password">
<mat-error class="error-message" *ngIf="accountDetailsForm.get('matching_passwords').get('password').hasError(validation.type) && (accountDetailsForm.get('matching_passwords').get('password').dirty || accountDetailsForm.get('matching_passwords').get('password').touched)">{{validation.message}}</mat-error>
</mat-error>
</mat-form-field>

<mat-form-field class="full-width">
<input matInput type="password" placeholder="Confirm Password" formControlName="confirm_password" [errorStateMatcher]="parentErrorStateMatcher" required>
<mat-error *ngFor="let validation of account_validation_messages.confirm_password">
<mat-error class="error-message" *ngIf="(accountDetailsForm.get('matching_passwords').get('confirm_password').hasError(validation.type)|| accountDetailsForm.get('matching_passwords').hasError(validation.type)) && (accountDetailsForm.get('matching_passwords').get('confirm_password').dirty || accountDetailsForm.get('matching_passwords').get('confirm_password').touched)">{{validation.message}}</mat-error>
</mat-error>
</mat-form-field>

</div>

<mat-checkbox formControlName="terms">
I accept terms and conditions
</mat-checkbox>
<mat-error *ngFor="let validation of account_validation_messages.terms">
<mat-error class="error-message" *ngIf="accountDetailsForm.get('terms').hasError(validation.type) && (accountDetailsForm.get('terms').dirty || accountDetailsForm.get('terms').touched)">{{validation.message}}</mat-error>
</mat-error>

<button class="submit-btn" color="primary" mat-raised-button type="submit" [disabled]="!accountDetailsForm.valid">
Submit
</button>

</form>

我的.ts文件代码

在这里,我为错误指定了错误消息

account_validation_messages = {
'username': [
{ type: 'required', message: 'Username is required' },
{ type: 'minlength', message: 'Username must be at least 5 characters long' },
{ type: 'maxlength', message: 'Username cannot be more than 25 characters long' },
{ type: 'pattern', message: 'Your username must contain only numbers and letters' },
{ type: 'validUsername', message: 'Your username has already been taken' }
],
'email': [
{ type: 'required', message: 'Email is required' },
{ type: 'pattern', message: 'Enter a valid email' }
],
'confirm_password': [
{ type: 'required', message: 'Confirm password is required' },
{ type: 'areEqual', message: 'Password mismatch' }
],
'password': [
{ type: 'required', message: 'Password is required' },
{ type: 'minlength', message: 'Password must be at least 5 characters long' },
{ type: 'pattern', message: 'Your password must contain at least one uppercase, one lowercase, and one number' }
],
'terms': [
{ type: 'pattern', message: 'You must accept terms and conditions' }
]
}

表单验证器

 this.accountDetailsForm = this.fb.group({
username: new FormControl('', Validators.compose([
UsernameValidator.validUsername,
Validators.maxLength(25),
Validators.minLength(5),
Validators.pattern('^(?=.*[a-zA-Z])(?=.*[0-9])[a-zA-Z0-9]+$'),
Validators.required
])),
email: new FormControl('', Validators.compose([
Validators.required,
Validators.pattern('^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+.[a-zA-Z0-9-.]+$')
])),
matching_passwords: this.matching_passwords_group,
terms: new FormControl(false, Validators.pattern('true'))
})

一切正常,但问题是我正在使用 ngFor 来显示错误,但我想在 mat-error 的帮助下一次显示一个错误。

最佳答案

使用CSS

首先隐藏所有错误

mat-error {
display: none;
}

然后只显示每个表单域的第一个错误:

mat-form-field mat-error:first-child {
display: block;
}

或者只有表单的第一个错误

form mat-error:first-child {
display: block;
}

关于angular - 我想用 react 形式和 Angular Material 一次显示一个错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51806607/

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