- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我知道其他时候也有人以类似的形式问过这个问题,但我的情况有点不同;让我解释一下:
我有一个重置密码表单,有 3 个字段(旧密码、新密码、确认密码)。当然,我需要检查提交的密码是否等于提交的确认密码,我想在单击提交之前执行此操作。我定义了如下形式(在 component.ts 中)
this.form = fb.group({
// define your control in you form
oldpassword: ['', Validators.required],
password: ['', [Validators.required, Validators.minLength(4), Validators.maxLength(10)]],
confirmPassword: ['', [Validators.required, Validators.minLength(4), Validators.maxLength(10)]]
}, {
validator: PasswordValidation.MatchPassword // your validation method
});
export class PasswordValidation {
static MatchPassword(AC: AbstractControl) {
const password = AC.get('password').value; // to get value in input tag
const confirmPassword = AC.get('confirmPassword').value; // to get value in input tag
if (password !== confirmPassword) {
AC.get('confirmPassword').setErrors({MatchPassword: true})
} else {
return null
}
}
}
这就是问题所在,如果我在编译“新密码”之前更改“确认密码字段”,则表单无效。换句话说,如果我尊重字段的垂直顺序,一切都可以,但如果我以不同的顺序编译字段,那么表单仍然无效。
这是 html 代码:(如您所见,它有点令人困惑,因为我有其他 UX 约束 ehich 对应于用户可能的交互)
<form [formGroup]="form" novalidate (ngSubmit)="modal.open()">
<div class="form-group">
<label for="oldpassword">Inserisci vecchia password</label>
<input type="password" id="oldpassword" class="form-control" formControlName="oldpassword">
<div
*ngIf="form.controls['oldpassword'].hasError('required') && form.controls['oldpassword'].touched"
class="alert alert-danger">
Campo obbligatorio
</div>
</div>
<div class="form-group">
<label for="password">Inserisci nuova password</label>
<input type="password" id="password" class="form-control" formControlName="password">
<div *ngIf="form.controls['password'].hasError('required') && form.controls['password'].touched"
class="alert alert-danger">
Campo obbligatorio
</div>
<div *ngIf="form.controls['password'].hasError('minlength') && form.controls['password'].touched"
class="alert alert-danger">
Lunghezza minima: 4 caratteri.
</div>
</div>
<div class="form-group">
<label for="confirmPassword">Ripeti nuova password</label>
<input type="password" class="form-control" id="confirmPassword" formControlName="confirmPassword">
<div class="alert alert-danger"
*ngIf="form.errors?.MatchPassword || (form.controls['confirmPassword'].hasError('required') && form.controls['confirmPassword'].touched)">
Le password non corrispondono
</div>
</div>
<div *ngIf="!this.state.controlloIn; else elseBlock">
<button type="submit" class="btn btn-primary btn-update" [disabled]="!form.valid || this.controlloInvia">
Invia Richiesta
</button>
</div>
<ng-template #elseBlock>
<button type="submit" class="btn btn-primary btn-update" [disabled]="true">
Invia Richiesta
</button>
</ng-template>
</form>
我不是 Angular 专家,所以如果你能给我建议其他最佳实践,我会非常高兴。感谢大家
最佳答案
您直接在确认字段中设置错误。如果您不再触摸该字段,则会在其上设置错误并且永远不会被删除。 Angular 仅在字段已被编辑时运行验证器,因为您可以合理地认为如果字段被标记为错误且未被编辑,它仍处于错误状态。
我建议您将此错误直接放在 FormGroup
上:
static MatchPassword(AC: AbstractControl) {
const password = AC.get('password').value; // to get value in input tag
const confirmPassword = AC.get('confirmPassword').value; // to get value in input tag
if (password !== confirmPassword) {
return {MatchPassword: true};
} else {
return null
}
}
<div class="alert alert-danger"
*ngIf="form.errors?.MatchPassword || (form.controls['confirmPassword'].hasError('required') && form.controls['confirmPassword'].touched)">
Le password non corrispondono
</div>
关于Angular 2匹配表单中的密码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47135277/
Internal Server Error: /admin/account/customuser/add/ Traceback (most recent call last): File "C:\
有问题!虽然我发现几乎相似的线程但没有帮助:( 我编写了一个 php 脚本来从我的 MySQL 数据库中获取注册用户的数量。该脚本在我的本地主机上运行良好;它使用给定的用户名、密码和主机名,分别是“r
我正在做一项基于密码的作业,我将 key 和消息放在单独的数组中。我想创建第三个数组,其中包含围绕消息大小的 key ,如下所示: message keykeyk 我已经在这个问题上苦苦挣扎了一段时间
我的几个客户要求我实现图形密码检查器,例如 关于如何实现这种 UI 有什么想法吗? 最佳答案 试着看看这个:https://code.google.com/p/android-lockpattern/
我正在使用 MAMP,每次登录 phpMyAdmin 时,都会收到以下错误/警告消息: the configuration file now needs a secret passphrase (bl
我正在尝试通过将 Visual Studio 2013 连接到我的测试机来调试 WDF 驱动程序。它创建一个名为 WDKRemoteUser 的用户,并在进行测试时尝试自动登录。有人知道这个用户的密码
使用具有指定用户名和密码的 SVN 提交。我希望服务器抛出错误;所以我可以告诉我的用户他/她的密码错误。 相反,在使用错误密码提交后: svn commit "test_file.txt" --use
我正在尝试实现 friend 推荐。 它从节点“你”开始。而且,我想找到节点“安娜”。 换句话说,这是我的两个或更多 friend 共同认识的人。上面的示例节点是 Anna。 如果您的帮助,我将不胜感
我都尝试过 wget --user=myuser --password=mypassword myfile 和 wget --ftp-user=myuser --ftp-password=mypass
我的一位用户提示说,每当他尝试使用默认管理界面(Django 的管理员)添加新用户(auth.User)时,新用户名和密码都会自动填充他自己的。 问题是他在登录时要求 Firefox 记住他的用户名/
我们正在开发一款应用(当然)用于应用购买 (IAP)。我已完成指南中的所有操作以启用 iap,并且一切正常,直到我想要购买为止。 部分代码: MainViewController.m -(vo
我试图创建两个可选匹配项的并集(如下所示),但我得到的不是并集,而是两者的交集。我应该如何更改此查询以获得所需的联合? optional match (a:PA)-[r2:network*2]-(b:
我想将Ansible用作另一个Python软件的一部分。在该软件中,我有一个包含其用户名/密码的主机列表。 有没有一种方法可以将SSH连接的用户/密码传递给Ansible ad-hoc命令或以加密方式
嗨,我在使用xampp的Apache Web服务器上收到错误500。直到我使用.htaccess,.htpasswd文件,错误才出现。我搜索了,但找不到语法错误。我只有1张图片和要保护的索引文件。以下
我一直使用它来编辑用户帐户信息: $this->validate($request, [ 'password' => 'min:6', 'password_confirmation'
我需要使用InstallUtil来安装C# Windows服务。我需要设置服务登录凭据(用户名和密码)。这一切都需要默默地完成。 有没有办法做这样的事情: installutil.exe myserv
已关闭。此问题不符合Stack Overflow guidelines 。目前不接受答案。 这个问题似乎不是关于 a specific programming problem, a software
如果我有一个随机的、16 个字符长的字母数字盐(不同大小写),它是为每个用户生成和存储的,我是否还需要一个站点范围的盐? 换句话说,这样好吗? sha1($user_salt . $password)
我正在开发一个空白程序,该程序将允许用户创建一个帐户,以便他们可以存储其余额和提款/存款。用户输入用户名和密码后,如何存储这些信息以便用户可以登录并查看其余额?我不一定要尝试使其非常安全,我只是希望能
我正在尝试寻找一种通用方法来搜索没有链接到另一个节点或节点集的节点或节点集。例如,我能够找到特定类型(例如 :Style)的所有节点,这些节点以某种方式连接到一组特定的节点(例如 :MetadataR
我是一名优秀的程序员,十分优秀!