javascript - Angular2 - Form Validation on conditionally created input field - Stack Overflow

I am creating a simple form where user can select two possible values i.e. Local and Foreigner. If user

I am creating a simple form where user can select two possible values i.e. Local and Foreigner. If user selects nothing it makes the value of the form invalid. If user selects Local the form bees valid. If user selects Foreigner, a new field appears for taking country as input from user which is also required. If user enters nothing in the country input field this makes the form invalid.

I have tried creating the form as below:

import {Component} from 'angular2/core';
import {bootstrap} from 'angular2/platform/browser';
import {Control,ControlGroup,FormBuilder,FORM_DIRECTIVES,Validators} from 'angular2/mon';

@Component({
  selector: 'app',
  template: `
  <form [ngFormModel]="form" >
    <select [ngFormControl]="nationalityCtrl" >
      <option *ngFor="#nationality of nationalities" [value]="nationality" >{{nationality}}</option>
    </select><br>
    <input *ngIf="form.value.nationality == 'Foreigner'" type="text" [ngFormControl]="countryCtrl" placeholder="Country Name" />
    <button class="btn btn-primary" [disabled]="!form.valid" >Submit</button>
  </form>
  `,
  directives: [FORM_DIRECTIVES]
})
class MainApp{
  public nationalities = ["Foreigner","Local"];
  public form:ControlGroup;
  public nationalityCtrl:Control;
  public countryCtrl:Control;

  constructor(private _fb:FormBuilder){
  var self = this;

  this.nationalityCtrl = new Control("",Validatorspose([Validators.required]));
  this.countryCtrl = new Control("",Validatorspose([function(control:Control){
      if(self.nationalityCtrl.value == "Foreigner" && !control.value){
        return {invalid: true};
      }
    }]));

    this.form = this._fb.group({
      nationality: this.nationalityCtrl,
      country: this.countryCtrl
    });
  }
}
bootstrap(MainApp);

But as soon as I select the Foreigner option I get the following error on console:

EXCEPTION: Expression '!form.valid in MainApp@6:36' has changed after it was checked. Previous value: 'false'. Current value: 'true' in [!form.valid in MainApp@6:36]

I re-produced the problem on plunker here you can see the error message on the console.

I am creating a simple form where user can select two possible values i.e. Local and Foreigner. If user selects nothing it makes the value of the form invalid. If user selects Local the form bees valid. If user selects Foreigner, a new field appears for taking country as input from user which is also required. If user enters nothing in the country input field this makes the form invalid.

I have tried creating the form as below:

import {Component} from 'angular2/core';
import {bootstrap} from 'angular2/platform/browser';
import {Control,ControlGroup,FormBuilder,FORM_DIRECTIVES,Validators} from 'angular2/mon';

@Component({
  selector: 'app',
  template: `
  <form [ngFormModel]="form" >
    <select [ngFormControl]="nationalityCtrl" >
      <option *ngFor="#nationality of nationalities" [value]="nationality" >{{nationality}}</option>
    </select><br>
    <input *ngIf="form.value.nationality == 'Foreigner'" type="text" [ngFormControl]="countryCtrl" placeholder="Country Name" />
    <button class="btn btn-primary" [disabled]="!form.valid" >Submit</button>
  </form>
  `,
  directives: [FORM_DIRECTIVES]
})
class MainApp{
  public nationalities = ["Foreigner","Local"];
  public form:ControlGroup;
  public nationalityCtrl:Control;
  public countryCtrl:Control;

  constructor(private _fb:FormBuilder){
  var self = this;

  this.nationalityCtrl = new Control("",Validators.pose([Validators.required]));
  this.countryCtrl = new Control("",Validators.pose([function(control:Control){
      if(self.nationalityCtrl.value == "Foreigner" && !control.value){
        return {invalid: true};
      }
    }]));

    this.form = this._fb.group({
      nationality: this.nationalityCtrl,
      country: this.countryCtrl
    });
  }
}
bootstrap(MainApp);

But as soon as I select the Foreigner option I get the following error on console:

EXCEPTION: Expression '!form.valid in MainApp@6:36' has changed after it was checked. Previous value: 'false'. Current value: 'true' in [!form.valid in MainApp@6:36]

I re-produced the problem on plunker here you can see the error message on the console.

Share asked Apr 4, 2016 at 10:15 EesaEesa 2,8592 gold badges37 silver badges53 bronze badges 1
  • No plunker works fine as expected. – Eesa Commented Apr 4, 2016 at 11:17
Add a ment  | 

1 Answer 1

Reset to default 7

I would define a global validator for the whole form since your validator depends on two fields:

this.form = this._fb.group({
  nationality: this.nationalityCtrl,
  country: this.countryCtrl
}, {
  validator: (control:Control) => {
    var nationalityCtrl = control.controls.nationality;
    var countryCtrl = control.controls.country;
    if(nationalityCtrl.value == "Foreigner" && !countryCtrl.value){
      return {invalid: true};
    }
  }
});

See this plunkr: https://plnkr.co/edit/Cksiv2UFbWoVJv2VwPwh?p=preview.

See this question for more details:

  • Cross field validation in Angular2

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744913885a4600715.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信