0

I have a component with a form that has some inputs with data validations How can I pre populate with the logged user's info?

userService.ts

public getCurrentUser():User {
  return this.currentUser;
}

component.ts

vendorInformationForm: FormGroup;
states = states;

constructor(
  private fb: FormBuilder,
  private userService: UserService) { }

ngOnInit() {
  this.initForm();
}

initForm() {
  this.vendorInformationForm = this.fb.group({
    name: [null],
    adress: ['', [Validators.required, Validators.maxLength(60)]],
    city: ['', Validators.maxLength(20)],
    state: ['', [Validators.required]],
    zip: ['', [Validators.required]],
    vendorNumber: [null],
    minimumOrderAmount: ['', [Validators.required, Validators.min(0) ,Validators.pattern('^[0-9]$')]],
    freightPrepaidAt: ['', [Validators.required]],
    preferredShipper: ['', [Validators.required]],
    email: ['', [Validators.required, Validators.email]]
  })
}

component.html

<div class="form-row">
    <div class="col-12">
        <div class="form-group">
            <label for="adress">Adress</label>
            <input type="text" id="adress" class="form-control" formControllName="adress"> 
        </div>
    </div>
</div> 

... and so on for every field

5 Answers 5

2

You can use

this.form.setValue({
  name: [response.name],
  adress: [response.address]
});

Replace accordingly to your response

Sign up to request clarification or add additional context in comments.

Comments

1

We can use setValue() or patchValue() to set data

Try like this:

let vendorData = {
   name : "Adrita",
   ...
}

this.vendorInformationForm.patchValue(vendorData)

If you have all the information, then use setValue otherwise use patchValue

Comments

1

What properties do the user have? In general you have the possibility to use the form-groups patchValue or setValue function

this.vendorInformationForm.patchValue(this.userService.getCurrentUser());

or

this.vendorInformationForm.setValue(this.userService.getCurrentUser());

The patchValue patches all fields that are matching between the values and the form configuration. The setValue function requires the data to be exactly of the formgroups form.

Comments

1

To update your form you can do it using the methods patchValue() or setValue(). This way:

component.ts

   const userInfo =  this.userService.getCurrentUser();

   this.vendorInformationForm.patchValue(userInfo);
 

Comments

0

When initializing your form with this.fb.group you can pass default data to your form inputs :

User user = this.getUser()
    this.vendorInformationForm = this.fb.group({
      name: [this.user.name ? this.user.name : ''],
      adress: [this.user.address ? this.user.address : '', [Validators.required, Validators.maxLength(60)]],
      email: [this.user.email ? this.user.email : '', [Validators.required, Validators.email]],
      ...
    })

If you want to update theses data after the initialization of the form, you can use the patchValue method :

this.vendorInformationForm.patchValue('yourinputname', 'thenewvalue')

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.