Home > front end >  angular form is always returning false/invalid
angular form is always returning false/invalid

Time:11-25

I have a form where I am checking if the form is invalid if yes it should just stop the application and if not do something else. But right now my form is always returning invalid = true and I can not find the error for some reason. Could someone look and tell me whats wrong

<form [formGroup]="form">
<input 
        formControlName="personNameField"
        type="text"
        placeholder="Bitte eingeben"
        [ngClass]="{'error': personNameField.errors}"
        ></input>
</form>

ts files with formcontrol:

    form = this.builder.group({
    personNameField: new FormControl('',
      [Validators.required]),
  });

method to check if form is valid/invalid:

  onSubmit() {
    this.form.markAllAsTouched();
    if (this.form.invalid) {
      return true;
    }
    else{
    return false;
  }

CodePudding user response:

  1. you added the Validators.required validator, this means you have to enter a text in your input otherwise the form is invalid.

  2. if this is not the error, please create an example on stackblitz and share the link

CodePudding user response:

I have just copy-pasted your code and it is working fine for me.

Here is working code : https://stackblitz.com

CodePudding user response:

thanks for everyone who helped me out. The error was a formcontrol not being rendered thus I could never enter a value so it was invalid

  • Related