简体   繁体   English

Ionic3中的表单验证

[英]Form Validation in Ionic3

  1. I had used angular form validation but It seems that it only works on HTML component and not for the ionic component. 我曾经使用过角形式验证,但似乎它仅适用于HTML组件,不适用于ionic组件。
  2. I had tried "formControlName" but it could not work in <ion-input> & also not throw any error. 我曾尝试过“ formControlName”,但它在<ion-input>无法使用,也不会引发任何错误。
  3. Here in the below code formControlName works fine with <textarea> 在下面的代码中,formControlName与<textarea>可以正常工作

    my .ts file: 我的.ts文件:

      constructor(private formBuilder: FormBuilder) { this.userForm = this.formBuilder.group({ 'name': ['', Validators.required], 'email': ['', Validators.required], 'profile': ['', Validators.required] }); this.userForm.get('name').disable(); this.userForm.get('email').disable(); this.userForm.get('profile').disable(); } edit() { this.userForm.get('name').enable(); this.userForm.get('email').enable(); this.userForm.get('profile').enable(); } 

In My .html File: 在我的.html文件中:

<form [formGroup]="userForm" (submit)="saveUser()">
  <ion-label>Name</label>
  <ion-input formControlName="name" id="name"></ion-input>


  <ion-label>Email</label>
  <ion-input formControlName="email" id="email"></ion-input>

  <label for="profile">Profile Description</label>
  <textarea formControlName="profile" id="profile"></textarea>

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

<button round ion-button (click)="edit()">Edit</button>

After so much digging for this issue i had finally found the solution: Also problem is from <ion-input> core the disabled property is sticked to it whether, we're trying to enable back "enable" wont work. 经过对这个问题的大量研究之后,我终于找到了解决方案:同样的问题是,从<ion-input>核心是否启用了禁用属性,我们是否试图启用“启用”功能 for that I am using this technique. 为此,我正在使用这项技术。

In .html file include: 在.html文件中,包括:

<div id="tag">....stuff provided question .html file.....</div>

In .ts file I wrote: 在.ts文件中,我写道:

edit()
{
   this.userForm.get('name').enable();
   this.userForm.get('email').enable();
   this.userForm.get('profile').enable();

   let field = document.getElementById("tag");
   let children = field.getElementsByTagName("input");

    for(var i=0;i<children.length;i++)
     {
      children[i].removeAttribute("disabled");
     }

} }

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

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