[英]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 个表单控制字段
email
、 password
和age
。 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();
禁用了我的email
和password
; 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我注意到的其他一些行为是
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. 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 个输入email
和password
,表单将保持无效,但如果我们更新表单中的值,这不会更改为有效。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
。
DISABLED
.DISABLED
。DISABLED
again cause all fields have this status.DISABLED
,因为所有字段都具有此状态。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.