【问题标题】:Compare cross field validation in formGroup in angular reactive forms以角度反应形式比较 formGroup 中的跨字段验证
【发布时间】:2019-01-01 16:41:10
【问题描述】:

我正在尝试创建一个包含字段 password 和 confirmPassword 的注册表单,我使用了角度反应表单,并且我使用了 @rxweb 包的验证我使用了跨字段 formGroup 我使用了 RxwebValidators 的比较验证

这是我的 component.ts 代码:

export class RegisterComponent implements OnInit {
  form: FormGroup;
  password = new FormControl("", [RxwebValidators.required()]);
  ConfirmPassword = new FormControl("", [RxwebValidators.compare({fieldName:'password '});

  constructor(private fb: FormBuilder) {
  }

  ngOnInit() {
    this.form = this.fb.group({
      "password": this.password,
      "ConfirmPassword ": this.ConfirmPassword 
    });
  }

我没有得到解决方案如何进一步解决这个问题

【问题讨论】:

  • 跨字段formgroup是什么意思?
  • 新年快乐,欢迎来到 stackoverflow 社区。请考虑让您的问题更加清晰。尝试添加示例/预期结果和当前行为。它将帮助我们和其他人学习和帮助。 :)
  • @avni-patel,你能告诉我什么不起作用吗?你有什么错误吗?考虑将您遇到的任何错误添加到您的问题中。

标签: angular angular-reactive-forms angular-validation


【解决方案1】:

我认为你的代码中有一些语法问题,下面的代码就像一个魅力

  form:FormGroup;

   constructor(private fb:FormBuilder){}

   ngOnInit(){
     this.form = this.fb.group({
        password : ['', RxwebValidators.required()],
        confirmPassword: ['', RxwebValidators.compare({fieldName:'password'})]
     });
   }

神殿

<form id="passwordInputForm" [formGroup]="form">
  <label for="password" >password:</label><input id="password" type="password" formControlName="password">
  <br/>
  <label for="confirmPassword" >confirm password:</label><input id="confirmPassword" type="password" formControlName="confirmPassword">
  <button type="submit" [disabled]="!form.valid">Submit</button>
</form>

【讨论】:

    猜你喜欢
    • 2022-11-03
    • 1970-01-01
    • 2018-12-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多