We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
close icon

Dropdownlist reactive form setValue not working

Hello,

I'm having a problem assigning a value to a reactive form control dropdownlist. Value is not set.

Form definition:

  private defineForm() {
    this.form = this.fb.group({
      userName: new FormControl(this.authService.getUser().userName, [Validators.required, Validators.maxLength(50)]),
      startDate: new FormControl(new Date(), [Validators.required]),
      endDate: new FormControl('', [Validators.required]),
      reason: new FormControl('', [Validators.required]),
      approved: new FormControl(false, [Validators.required])
    });
  }

  get userName() { return this.form.get('userName'); }
  get startDate() { return this.form.get('startDate'); }
  get endDate() { return this.form.get('endDate'); }
  get reason() { return this.form.get('reason'); }
  get approved() { return this.form.get('approved'); }


This part works fine. Default value is assigned.

The next part is not working:

this.userName.setValue(this.absence.userName);

No value is assigned. If I look for this.userName, I see property 'value' with old value.

Any idea?
Regards
Jose


3 Replies

NP Narayanasamy Panneer Selvam Syncfusion Team October 24, 2019 05:01 PM UTC

Hi Jose, 
Greetings from Syncfusion Support. 
We have validated your requirement. Yes, we can update dropDownlist value through reactive form setValue method.  
 
Code example: 
App.component.html 
<ejs-dropdownlist formControlName="skillname" name="skillname" [dataSource]='autoreactiveskillset' 
    [placeholder]='autoreactiveplaceholder'> 
</ejs-dropdownlist> 
 
<button style="float:right" type="button" (click)="setDefaultValue()">Set Value</button> 
 
 
app.component.ts 
 public autoreactiveskillset: string[] = [ 
  'ASP.NET''ActionScript''Basic', 
  'C++''C#''dBase''Delphi', 
  'ESPOL''F#''FoxPro''Java', 
  'J#''Lisp''Logo''PHP' 
]; 
 
setDefaultValue() { 
  this.userForm.setValue({ name: 'John', skillname: 'Delphi', age: 20 }); 
} 
 
We have prepared a sample to update dropdown list value using button click. Please get it from below link, 
Also, we would like to inform you that the DropDown List will accept only accept already present in referred data source. 
 
Kindly let us know if you need further assistance on this.

Regards,
Narayanasamy P.
 



JL Jose Luis Garcia October 28, 2019 12:05 PM UTC

Hi Narayanasamy,

Thank you very much for your support.

I've tested agains your code and now is working fine.

Regards

Jose




SP Sureshkumar P Syncfusion Team October 29, 2019 09:00 AM UTC

Hi Jose, 
 
Most welcome. Please let us know if you require any further assistance. 
 
Regards, 
Sureshkumar P 


Loader.
Live Chat Icon For mobile
Up arrow icon