简体   繁体   English

Angular 反应式表单 - 禁用所需的表单控件使表单有效,即使没有给出值

[英]Angular reactive form - Disabling a required form control makes the form valid even if no value is given

I have created an angular reactive form like below我创建了一个 angular 反应形式,如下所示

component.html组件.html

<form [formGroup]="loginForm" (ngSubmit)="onSubmit()">
  <h5 class="card-title text-center">Login</h5>
  <div class="form-group">
    <label class="text-success" for="exampleInputEmail1"
      >Email address</label
    >
    <input
      type="email"
      class="form-control"
      id="exampleInputEmail1"
      aria-describedby="emailHelp"
      formControlName="email"
      placeholder="Enter email"
    />
  </div>
  <div class="form-group">
    <label class="text-danger" for="exampleInputPassword1"
      >Password</label
    >
    <input
      type="password"
      class="form-control"
      id="exampleInputPassword1"
      formControlName="password"
      placeholder="Password"
    />
  </div>
  <button type="submit" class="btn btn-primary">Login</button>
</form>

component.ts组件.ts

constructor(private fb: FormBuilder) {}

public loginForm: FormGroup;

ngOnInit(): void {
  this.initializeForm();
}

initializeForm = () => {
  this.loginForm = this.fb.group({
    age: [''],
    email: ['', [Validators.required]],
    password: ['', [Validators.required]],
  });
  this.loginForm.controls.email.disable();
  this.loginForm.controls.password.disable();
}

onSubmit() {
  if (this.loginForm.valid) {
    console.log('Login form - valid');
  } else {
    console.log('Login form - invalid');
  }
}

Fiddle having the problem 小提琴有问题

As you can see I have 3 form control fields inside the form group, email , password and age .如您所见,我在表单组中有 3 个表单控制字段emailpasswordage age is a hidden field, I will update this from my script based on some logic. age是一个隐藏字段,我将根据一些逻辑从我的脚本中更新它。

I have made my email and password disabled using this.loginForm.controls.email.disable();我使用this.loginForm.controls.email.disable();禁用了我的emailpassword and this.loginForm.controls.password.disable();this.loginForm.controls.password.disable(); during the initialization process itself.在初始化过程本身。 I will enable them depending on some logic later.稍后我将根据某些逻辑启用它们。

As of now I have 2 required form controls disabled and have not initialized with any value.截至目前,我禁用了 2 个必需的表单控件,并且尚未使用任何值进行初始化。 Ideally this form is not valid.理想情况下,此表格无效。 But when I try to log the form valid status in the console from the form submit function, it logs the valid status as true.但是,当我尝试从表单提交 function 在控制台中记录表单有效状态时,它将有效状态记录为 true。

Why is this so?为什么会这样?

Some other behaviors that I noticed are我注意到的其他一些行为是

  • Instead of disabling independent form controls, if we disable the complete form using this.loginForm.disable();如果我们使用this.loginForm.disable(); , the form will stay in invalid state as per my requirement, but this will not be changed to valid if we update the value in form. ,根据我的要求,表格将保持无效 state ,但如果我们更新表格中的值,这不会更改为有效。
  • Also if I disable the third input age along with the 2 inputs email and password , the form will stay invalid, but this will not be changed to valid if we update the value in form.此外,如果我禁用第三个输入age以及 2 个输入emailpassword ,表单将保持无效,但如果我们更新表单中的值,这不会更改为有效。
  • Also if did not have the third field age and I made 2 inputs email and password disabled, the form will stay invalid, but still this will not be changed to valid if we update the value in form.此外,如果没有第三个字段age并且我输入了 2 个输入email并禁用password ,则表单将保持无效,但如果我们更新表单中的值,这仍然不会更改为有效。

Can someone say why the form is valid when some inputs are disabled?有人可以说为什么禁用某些输入时表单有效吗?

When you disable a form field (form control) it means that the control is exempt from validation checks and excluded from the aggregate value of any parent.当您禁用表单字段(表单控件)时,这意味着该控件免于验证检查并从任何父级的聚合值中排除。 Status will be DISABLED .状态将是DISABLED

  • So in your project, when you disable email/password, and age is still there then your form will be valid cause age is valid.因此,在您的项目中,当您禁用电子邮件/密码并且年龄仍然存在时,您的表单将有效,因为年龄有效。
  • In your first bullet, when you disable the whole form, you check if form is valid.在您的第一个项目符号中,当您禁用整个表单时,您检查表单是否有效。 Form is not valid nor invalid.表格无效也不无效。 Your form will have status DISABLED .您的表单将具有状态DISABLED
  • In the second bullet, if you disable all the fields of the form, then form will have status DISABLED again cause all fields have this status.在第二个项目符号中,如果禁用表单的所有字段,则表单将再次具有状态DISABLED ,因为所有字段都具有此状态。
  • In the third bullet, both fields disabled means that your form will have DISABLED status.在第三个项目符号中,两个字段都禁用意味着您的表单将处于DISABLED状态。

In other words, checking for valid and invalid form is not correct.换句话说,检查有效和无效形式是不正确的。 Try to use else if and add DISABLED check for better understanding.尝试使用else if并添加DISABLED检查以便更好地理解。

  if (this.loginForm.valid) {
    console.log('Login form - valid');
  } else if (this.loginForm.invalid) {
    console.log('Login form - invalid');
  } else if (this.loginForm.disabled) {
    console.log('Login form - disabled');
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM