Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

214
Views
How to get the form status after rendering (FormBuilder)

I am using FormBuilder to generate form. On ngOnInit, I am calling a function which generates a login form. If any user saved their credentials, next time when they try to login these fields will be pre-filled with their saved credentials (username and password).

I want to get the status of form after rendering. I called ngAfterViewInit lifecycle hook but got INVALID status of the form even it has username and password are pre-filled.

Below is the code:

constructor(private formBuilder: FormBuilder) {}

ngOnInit() {
  this.generateForm();
}

generateForm() {
  this.loginForm = this.formBuilder.group({
    username: [null, [Validators.required]],
    password: [null, [Validators.required]],
 });    
}

ngAfterViewInit() {
  console.log(this.loginForm.status);   //INVALID
}

Didn't understand why it is showing INVALID. Is there any other lifecycle hook in which I can get the VALID status of the form if it's pre-filled?

Looking forward for the solution. Thanks in advance!

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

It's not possible to know the autofilled values until the user interacts with the page. (security feature of Chrome)

You can try this workaround and have a reference to the fields:

 @ViewChild('usernameField')
 usernameField: ElementRef;

 @ViewChild('passwordField')
 passwordField: ElementRef;

 ngAfterViewInit(): void {
     setTimeout(() => this.setValues(), 200);
 }

 setValues(): void {
this.loginForm.get('login').setValue(this.usernameField.nativeElement.value);
this.loginForm.get('password').setValue(this.passwordField.nativeElement.value);
 }
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!