2

I am trying to set an input value via form group using patchValue() but the input still remains blank, this is my code sample...

component.html

<form [formGroup]="createStoreForm" (ngSubmit)="createStore()" novalidate>
  <label for="user">Unique User ID</label>
    <input type="text" name="user" class="form-control" [(ngModel)]="storeProfile.user" [formControl]="createStoreForm.controls['user']" readonly>
    <div *ngIf="createStoreForm.controls['user'].hasError('required') && formSubmitAttempt" class="form-error">Store user is required</div>
</form>

component.ts

export class storeProfileComponent implements OnInit {
  storeProfile: any = {};
  userData: any = {};
  userId: any;
  createStoreForm: FormGroup;
  formSubmitAttempt: boolean;

  constructor(fb: FormBuilder, private storeSrv: StoreService, private router: Router) {
    this.createStoreForm = fb.group({
      'user': ['', Validators.required],
    });
  }

  ngOnInit() {
    let tempUser = localStorage.getItem('user');
    if (tempUser) {
      this.userData = JSON.parse(tempUser);
      this.userId = this.userData.id;
    };

  this.createStoreForm.patchValue({
    'user': this.userId.id,  // this.userId.id = undefined here
  });
 }
}

What is the right way to pass value from localStorage to the input field on the form?

CE0
  • 191
  • 3
  • 7
  • 18
  • first you set `this.userId = this.userData.id` and then again you set the form with `this.userId.id`. which is effectiely `this.userData.id.id`. are you sure thats correct? – dee zg May 21 '18 at 11:20
  • thanks for noticing, you are right but changing that didn't fix the issue tho. @deezg – CE0 May 21 '18 at 11:29

2 Answers2

1

You need to set value after getting value from local storage like this,

Try this -

export class storeProfileComponent implements OnInit {
  storeProfile: any = {};
  userData: any = {};
  userId: any;
  createStoreForm: FormGroup;
  formSubmitAttempt: boolean;

  constructor(public fb: FormBuilder, private storeSrv: StoreService, private router: Router) { }

  ngOnInit() {
    let tempUser = localStorage.getItem('user');
    if (tempUser) {
      this.userData = JSON.parse(tempUser);
      this.userId = this.userData.id;
    };

    this.createStoreForm = this.fb.group({
      'user': [this.userId.id, Validators.required],
    });
 }
}

<form [formGroup]="createStoreForm" (ngSubmit)="createStore()" novalidate>
  <label for="user">Unique User ID</label>
    <input type="text" name="user" class="form-control" formControl="user" readonly>
    <div *ngIf="createStoreForm.controls['user'].hasError('required') && formSubmitAttempt" class="form-error">Store user is required</div>
</form>
Pardeep Jain
  • 84,110
  • 37
  • 165
  • 215
  • error thrown here `this.createStoreForm = fb.group({'user': [this.userId.id, Validators.required]})` `cannot find name fb` – CE0 May 21 '18 at 11:25
  • opps !! just forgot to add `this`, try my updated answer. – Pardeep Jain May 21 '18 at 11:26
  • yeah i tried that already and error was `Property 'fb' does not exist on type 'StoreProfileComponent'` :( – CE0 May 21 '18 at 11:30
1

Just remove ngModel and use createStoreForm.value to get from values

<form [formGroup]="createStoreForm" (ngSubmit)="createStore()" novalidate>
  <label for="user">Unique User ID</label>
    <input type="text" name="user" class="form-control" [formControl]="createStoreForm.controls['user']" readonly>
    <div *ngIf="createStoreForm.controls['user'].hasError('required') && formSubmitAttempt" class="form-error">Store user is required</div>
</form>

export class storeProfileComponent implements OnInit {
  storeProfile: any = {};
  userData: any = {};
  userId: any;
  createStoreForm: FormGroup;
  formSubmitAttempt: boolean;

  constructor(fb: FormBuilder, private storeSrv: StoreService, private router: Router) {
    this.createStoreForm = fb.group({
      'user': ['', Validators.required],
    });
  }

  ngOnInit() {
    let tempUser = localStorage.getItem('user');
    if (tempUser) {
      this.userData = JSON.parse(tempUser);
      this.userId = this.userData.id;
    };

  this.createStoreForm.patchValue({
    'user': this.userId.id,  
  });
 }

 public createStore(){
  console.log(this.createStoreForm.value); // get from values 
 }
}
Muhammed Albarmavi
  • 23,240
  • 8
  • 66
  • 91