简体   繁体   English

如果反应表单字段无效,则禁用兄弟按钮Angular 5

[英]Disable a sibling button if reactive form field is not valid Angular 5

I have a component with a reactive form in it like so... 我有一个带有反应形式的组件,就像这样......

form component.html form component.html

<form [formGroup]="form" class="do-form">
    <div formGroupName="dot">
        <div class="do-form__container">
            <div class="do-form__group">
                <label for="day">Day</label>
                <input id="day" type="number" placeholder="XX" class="do-form__group__control" formControlName="day" />
            </div>
            <div class="do-form__group">
                <label for="month">Month</label>
                <input id="month" type="number" placeholder="XX" class="do-form__group__control" formControlName="month" />
            </div>
            <div class="do-form__group">
                <label for="year">Year</label>
                <input id="year" type="number" placeholder="XXXX" class="do-form__group__control" formControlName="year" />
            </div>
        </div>
        <div class="do-form__errors" *ngIf="isValid()">
            <p>Please enter a valid date</p>
        </div>
    </div>
</form>

and in my form.component.ts 并在我的form.component.ts

form = this.fb.group({
dot: this.fb.group({
  day: ['',
    [
      Validators.required,
      Validators.min(1),
      Validators.max(31)
    ]],
  month: ['',
    [
      Validators.required,
      Validators.min(1),
      Validators.max(12)
    ]],
  year: ['2018',
    [
      Validators.required,
      Validators.min(1918),
      Validators.max(2018)
    ]]
})
});

isValid() {
return (
  !this.form.valid &&
  this.form.get('dot.day').touched &&
  this.form.get('dot.month').touched &&
  this.form.get('dot.year').touched
);
}

Now I have a separate page (app.component.html) like so 现在我有一个单独的页面(app.component.html)

<app-form #formTool></app-form>
        <button class="full-width-btn" (click)="next(); form.sendResult();">SEND</button>

app.component.ts app.component.ts

import {  formComponent } from '../form-component/form-component.ts'

export ...

@ViewChild(formComponent) form;

Now basically I want to disable the send button until the form in the app form component is valid. 现在基本上我想禁用发送按钮,直到app表单组件中的表单有效。

I'm not entirely sure how to do this. 我不完全确定如何做到这一点。 I thought about storing a valid event on a shared server but I'm not sure how I can store a valid event in a service. 我考虑过在共享服务器上存储valid事件,但我不确定如何在服务中存储valid事件。 I saw that with non-reactive forms you can just have a button that uses the same ngModel but once again not sure if that would work in this case. 我看到使用非反应性表单,你可以只使用一个使用相同ngModel的按钮,但再一次不确定在这种情况下是否可行。

Any help would be appreciated! 任何帮助,将不胜感激!

EDIT 编辑

I have tried [disabled]="form.invalid" and [disabled]="!isValid()" but I am still able to click the button 我试过[disabled]="form.invalid"[disabled]="!isValid()"但我仍然可以点击按钮

I have also tried using [disabled]=!form.isValid() and [disabled]=!form.form.isValid() 我也试过使用[disabled]=!form.isValid()[disabled]=!form.form.isValid()

Thanks 谢谢

You are really close. 你真的很亲密。 Here is the only thing you need to add: 这是您需要添加的唯一内容:

 <app-form #formTool></app-form> <button class="full-width-btn" [disabled]="!isValid()" (click)="next(); form.sendResult();">SEND</button> 

In form component you could define an event @Output formValid = new EventEmitter(). 在表单组件中,您可以定义一个事件@Output formValid = new EventEmitter()。

Then you can listen to changes in input fields (on keypress or so) and on any change check the validity and if the form is valid, emit an event: formValid.emit(). 然后你可以听取输入字段的变化(在按键上等)和任何变化检查有效性,如果表格有效,则发出一个事件:formValid.emit()。

In the app component define formIsValid = false, and on the app-form element you can listen to the formValid event: < app-form (formValid)="formIsValid = true"> (or some function in app.component.ts instead of the inline code). 在app组件中定义formIsValid = false,在app-form元素上你可以监听formValid事件:<app-form(formValid)=“formIsValid = true”>(或app.component.ts中的某个函数而不是内联代码)。

And finally on button < button [disabled]="!formIsValid"> 最后在按钮<按钮[disabled] =“!formIsValid”>上

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

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