简体   繁体   English

我的表单无效,但我的 (ngSubmit) 正在调用 function

[英]My form is invalid but my (ngSubmit) is calling the function

I have one formGroup and inside i have one submit button:我有一个 formGroup,里面有一个提交按钮:

<form [formGroup]="myGroup" (ngSubmit)="submitForm()">
   <input type="text" formControlName="name">
   <button type="submit">Submit</button>
</form>

When my component init i call this function to build the form:当我的组件初始化时,我调用这个 function 来构建表单:

buildForm() {
  this.myGroup = new FormGroup({
    name: new FormControl(null, [Validators.required]),
  });
}

When i don't fill the input and click in the button, my component is calling the submitForm() .当我不填写输入并单击按钮时,我的组件正在调用submitForm() I put a console.log(myGroup.valid) and it is false , but still calls the (ngSubmit) event.我放了一个console.log(myGroup.valid)并且它是false ,但仍然调用(ngSubmit)事件。
I have other components and seems that when the form is invalid, Angular automatically don't call the submit function when the user click in the submit button.我有其他组件,似乎当表单无效时,当用户单击提交按钮时,Angular 自动不调用提交 function。 I don't know if i'm forgetting something to Angular do this automatic check, or i really need to do something like in my submit function, like:我不知道我是否忘记了 Angular 做这个自动检查,或者我真的需要在我的提交 function 中做类似的事情,比如:

if(this.myGroup.valid) {}

Do not start with an enabled button, I prefer to not show any button until validation is good.不要从启用的按钮开始,我宁愿在验证良好之前不显示任何按钮。

You can hook up a change listener like this:您可以像这样连接更改侦听器:

fromGroup.changes(changes=>{
  if (changes=="VALID"){ showMyButton(); }
})

The most likely reason for you to see different behavior from your forms in different components is use of both template forms and reactive forms in your project.您看到 forms 在不同组件中的不同行为的最可能原因是在您的项目中同时使用模板 forms 和反应式 forms。 The Angular Forms Introduction describes the key differences between the two form types. Angular Forms 简介描述了两种表单类型之间的主要区别。


First Approach第一种方法

Disable the submit button in the template AND double check input in the component.禁用模板中的提交按钮并仔细检查组件中的输入。

As other commenters have suggested, with reactive forms you will need to disable your submit button until the form data is valid.正如其他评论者所建议的那样,使用反应式 forms 您将需要禁用提交按钮,直到表单数据有效。

<button type="submit" [disabled]="!myGroup.valid">Submit</button>

What everyone else has failed to mention is that when you include (ngSubmit) in your form tag it also allows submitting the form with the enter key.其他人没有提到的是,当您在表单标签中包含(ngSubmit)时,它还允许使用 enter 键提交表单。 So, even if your submit button is disabled invalid form values can be submitted when the user clicks enter.因此,即使您的提交按钮被禁用,用户点击回车时也可以提交无效的表单值。

You can easily abort when enter is clicked and form data is invalid with one (poorly formatted) line of code:单击输入时,您可以轻松中止,并且表单数据对于一行(格式错误)的代码无效:

submitForm() {
  if (!myGroup.valid) { return; }
  // Your form submission logic
}

Second Approach第二种方法

The other option is to remove (ngSubmit) from your form tag, then remove type="submit" from your submit button and add (click) to your submit button.另一种选择是从您的表单标签中删除(ngSubmit) ,然后从您的提交按钮中删除type="submit"并添加(click)到您的提交按钮。

<button [disabled]="!myGroup.valid" (click)="submitForm()">Submit</button>

This removes the ability to submit the form with the enter key, which may be desirable in some cases but not in others.这消除了使用 enter 键提交表单的能力,这在某些情况下可能是可取的,但在其他情况下则不是。

When logging into an application users probably expect to be able to press enter to submit their credentials.登录到应用程序时,用户可能希望能够按 Enter 提交他们的凭据。 On pages that may have multiple forms it may become confusing exactly which form the enter key is triggering.在可能有多个 forms 的页面上,可能会混淆究竟是哪种形式的回车键正在触发。

You should still double check that the form is valid in your component logic since it is easy for a savvy user to enable buttons on your page with browser tools.您仍然应该仔细检查表单在您的组件逻辑中是否有效,因为精明的用户很容易使用浏览器工具在您的页面上启用按钮。

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

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