0

I have a component with form for creating and updating the user. I get information about user to update through the input parameter @Input('user') user. Also I wanna use this parameter to dynamicly paste a data about selected user to the reactive form. I created form like this one:

@Input('user') user = new User();      
form: FormGroup = this.fb.group({
        id: [{ value: this.user.id, disabled: true }],
        login: [this.user.login],
        password: [{ value: '', disabled: true }, [Validators.required, Validators.minLength(5), Validators.maxLength(100)]],
        userName: [this.user.userName, [Validators.required, Validators.minLength(2), Validators.maxLength(100)]],
        email: [this.user.email, [Validators.email]],
        lastLoginDate: [this.user.lastLoginDate],
        loginAttempts: [this.user.loginAttempts],
        passwordChangeDate: [this.user.passwordChangeDate],
        passwordChangeRequired: [this.user.passwordChangeRequired],
        roles: this.fb.array([this.user.roles]),
        isActive: [this.user.isActive]
      });

I expected the form control values have to change dynamicly when the user is updated. But despite user is updated, values at form are still the same.

Form photo

1 Answer 1

1

Via the javascript setter method, you can rebuild the form when a new user is set.

public user = new User();
@Input('user') set setUser(value){
  this.user = value;
  this.createForm();
}

public form: FormGroup;

ngOnInit(){
  this.createForm();
}

private createForm(){
  this.form = this.fb.group({ ... });
}
Sign up to request clarification or add additional context in comments.

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.