Angular2仅在某些条件下验证表单

Angular2仅在某些条件下验证表单,angular,typescript,Angular,Typescript,我只想在创建期间创建一个用户表单,但不想更新 这是我的验证逻辑 this.userform = this._formbuilder.group({ first_name: ['', Validators.required], last_name: ['', Validators.required], username: ['', Validators.compose( [ Validators.required,Validators.minLength(5

我只想在创建期间创建一个用户表单,但不想更新

这是我的验证逻辑

    this.userform = this._formbuilder.group({
  first_name: ['', Validators.required],
  last_name: ['', Validators.required],
  username: ['', Validators.compose(
    [
      Validators.required,Validators.minLength(5)
    ]
  )],
  password: ['',Validators.compose([
    Validators.required,ValidationService.passwordValidator
  ])],
  email: ['', Validators.compose([
    Validators.required,ValidationService.emailValidator
  ])],
  role: ['', Validators.required],

})
我只想在创建期间验证密码,也就是当newuser变量为true时,所以我尝试了

    this.userform = this._formbuilder.group({
  .....

  password: ['',

       (this.newUser) ?Validators.compose([
          Validators.required,ValidationService.passwordValidator
          ]):"" 

       ],
....
})
上述操作失败并返回错误

rror: Error in ./UsersComponent class UsersComponent - inline  
    template:113:51 caused by: 
   v is not a function
   Error: Error in ./UsersComponent class UsersComponent -
     inline template:113:51 caused by: v is not a function

当用户需要更改其配置文件时,为什么不想验证表单?这意味着用户在更新个人资料后,可以没有用户名、密码和电子邮件?这不是逻辑

您可以创建一个以编程方式检查所需的密码并调用passwordValidator(如果this.users可用)将其设置为唯一的验证器

您可以首先设置:

  this.userform = this._formbuilder.group({
  .....

  password: [''] //no initial validators,
....
})
在this.newUser设置为true的函数中,使用


this.newUser集在哪里?尝试传递null而不是:this.newUser?验证程序。编写…:Null此表单用于用户crud操作,创建用户时使用默认密码,但管理员在更新用户凭据时可以更新密码或无法更新密码,因此密码在更新过程中不是强制性的,但在创建过程中是强制性的。。。。希望它有意义,它有意义!在这种情况下,请查看以下帖子:
this.userform.setControl("password",new FormControl('',Validators.compose([
          Validators.required,ValidationService.passwordValidator
          ]));