【问题标题】:How to do validation with mat-checkbox?如何使用 mat-checkbox 进行验证?
【发布时间】:2018-11-28 20:02:41
【问题描述】:

我想知道如何检查复选框是否被选中,如果未选中,则会为用户显示错误消息。 我设法输入了错误消息,但无法将其与表单的其余部分一起使用。 你能帮助我吗? 这是我必须做的。

<mat-checkbox class="hcs-full-width" [formControl]="termsFormControl"> Aceite os termos de uso

</mat-checkbox>
<mat-error *ngIf="termsFormControl.hasError('required')">
    Termo é
    <strong>requirido</strong>
</mat-error>

.ts

export class AppComponent implements OnInit {

  private subscription: Subscription;
  uri: string;
  ssid: string;
  sessiondId: string;
  ip: string;
  mac: string;
  ufi: string;
  mgmtBaseUrl: string;
  clientRedirectUrl: string;
  req: string;
  userName: string;
  hmac: string;

  name: string;
  email: string;

  constructor(@Inject(DOCUMENT) private document: any, private route: ActivatedRoute) {
  }

  ngOnInit() {
    this.route.queryParams
      .filter(params => params.mac)
      .subscribe(params => {
        console.log(params);

        this.ssid = params.ssid;
        this.sessiondId = params.sessionId;
        this.ip = params.ip;
        this.mac = params.mac;
        this.ufi = params.ufi;
        this.mgmtBaseUrl = params.mgmtBaseUrl;
        this.clientRedirectUrl = params.clientRedirectUrl;
        this.req = params.req;
        this.hmac = params.hmac;
      });
      console.log("LOGAR: " + this.nameFormControl.valid);
    console.log("LOGAR: " + this.termsFormControl.valid);
  }

  Logar() {
    if (this.nameFormControl.valid && this.emailFormControl.valid && this.termsFormControl.valid) {
      this.userName = this.name + ";" + this.email;
      this.uri = "#" + this.ssid + "&sessionId=" + this.sessiondId + "&ip=" + this.ip + "&mac=" + this.mac + "&ufi=" + this.ufi + "&mgmtBaseUrl=" + this.mgmtBaseUrl + "&clientRedirectUrl=" + this.clientRedirectUrl + "&req=" + this.req + "&username=" + this.userName + "&hmac=" + this.hmac;
      // console.log("LOGAR: " + this.nameFormControl.valid);
      this.document.location.href = this.uri;
    }
    console.log("LOGAR: " + this.nameFormControl.valid);
    console.log("LOGAR: " + this.termsFormControl.valid);
  };

  emailFormControl = new FormControl('', [
    Validators.required,
    Validators.email,
  ]);

  nameFormControl = new FormControl('', [
    Validators.required,
  ]);

  termsFormControl = new FormControl('', [
    Validators.required,
  ]);

}

【问题讨论】:

    标签: javascript angular typescript angular-material2


    【解决方案1】:

    使用内置验证器:Validators.requiredTrue

    【讨论】:

      【解决方案2】:

      复选框不能这样工作,您必须实现自定义验证器并分配给您的termsFormControl。在您的情况下,它将是:

      termsFormControl = new FormControl('', [(control) => {    
          return !control.value ? { 'required': true } : null;
        }]
      );
      

      这里自定义验证器显式检查控件的值,如果为 false,它将设置 required 错误为 true。通过这种方式,您可以为表单设置状态并能够使用强大的功能。

      请参阅 GitHub 上的 issue 以了解为什么必须实施自定义验证的更多说明。

      已编辑 1

      要在单击按钮时显示错误消息,您可以将条件设​​置为在复选框无效和脏时出现错误:

      <mat-error *ngIf="termsFormControl.invalid && termsFormControl.dirty">
              Termo é <strong>requirido</strong>
      </mat-error>
      

      然后在单击按钮时,您可以将复选框设置为脏(我没有看到您的整个代码,但应该是这样的):

      onSubmit() {
        this.form.get('termsFormControl ').markAsDirty();
      }
      

      已编辑 2

      基于validator 中内置的@Julida 建议,Validators.requiredTrue 可用于复选框元素。它验证复选框是选中还是未选中。如果未选中则标记控件无效。

      【讨论】:

      • kat1330 感谢您的回复,但我想知道如何使错误消息仅在用户单击按钮且未选中复选框时出现。
      • @Mark 我更新了答案。基本上,如果控件无效和脏,我在 HTML 中扩展大小写以显示消息。然后单击按钮,我将控件标记为脏。
      • @kat1330 为什么不使用内置验证器“requiredTrue”? termsFormControl = new FormControl(false, Validators.requiredTrue)
      【解决方案3】:
      <mat-checkbox 
       (change)="todoItemSelectionToggle($event)">{{node.item}}</mat-checkbox>
      
      
              In .ts file if event.checked is true checkbox is selected else viceversa
      
      
               todoItemSelectionToggle(event)
               {
               console.log = event.checked;
               }
      

      【讨论】:

        猜你喜欢
        • 2019-05-19
        • 2020-06-03
        • 2021-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-08-24
        • 2020-07-09
        • 2011-01-20
        • 2012-03-02
        相关资源
        最近更新 更多