0

I use angular 8.

Here is html code:

   <div class="row mt-5">
    <div class="col-md-6 mx-auto">
        <h2 class="text-center">Add fridge type</h2>
        <div class="card mt-3">
            <div class="card-body">
              <form [formGroup]="newFridgeType" (ngSubmit)="onSubmit()">
                <div class="form-group">
                  <label class="col-md-4">Title </label>
                  <input type="text" class="form-control" formControlName="text" />
                  <label class="col-md-4">Description </label>
                  <input type="text" class="form-control" formControlName="description" />
                  </div>
                  <div class="form-group">
                    <button type="submit" class="btn btn-primary col-md-4" [disabled]="todoForm.invalid">Add</button>
                    <a [routerLink]="['/fridgetypeList']">
                    <button type="submit" class="btn btn-primary col-md-4 ml-1">Back</button>
                  </a>
                  </div>
              </form>
            </div>
        </div>
    </div>
  </div>

And here is component defenition:

import { Component, OnInit } from '@angular/core';
import {FormBuilder, FormGroup, Validators} from "@angular/forms";
import { FridgeTypeService } from './fridgeType.service';
import { Router } from "@angular/router";

@Component({
  selector: 'fridgeType-add',
  templateUrl: './fridgeType-add.component.html',
  //styleUrls: ['./todo-add.component.css']
})

export class FridgeTypeAddComponent implements OnInit {

  newFridgeType: FormGroup;
  constructor(private formBuilder: FormBuilder, 
              private router: Router, 
              private fridgeTypeService: FridgeTypeService) 
              { }


  ngOnInit() {
    this.newFridgeType = this.formBuilder.group({
      text: ['', Validators.required],
      description: ['', Validators.required]
    });
  }

  onSubmit() {
    debugger;
    this.fridgeTypeService.save(this.newFridgeType.value)
      .subscribe(res => {
          let id = res['_id'];
          this.router.navigate(['/fridgetypeList']);
        }, (err) => {
          console.log(err);
        });
  }
}

In console I on this row:

<input type="text" class="form-control" formControlName="text" />

I get this error:

ERROR TypeError: Cannot read property 'invalid' of undefined
    at Object.eval [as updateRenderer] (FridgeTypeAddComponent.html:11)
    at Object.debugUpdateRenderer [as updateRenderer] (core.js:45294)
    at checkAndUpdateView (core.js:44277)
    at callViewAction (core.js:44637)
    at execComponentViewsAction (core.js:44565)
    at checkAndUpdateView (core.js:44278)
    at callViewAction (core.js:44637)
    at execEmbeddedViewsAction (core.js:44594)
    at checkAndUpdateView (core.js:44272)
    at callViewAction (core.js:44637)

Any idea why I get error above?

2
  • I guess the error comes from todoForm.invalid What is todoForm supposed to be? Commented Nov 27, 2019 at 17:07
  • what is todoForm - where this is defined in class file Commented Nov 27, 2019 at 17:08

4 Answers 4

2

Use newFridgeType for disabled attribute in your HTML instead ot todoForm

<button type="submit" class="btn btn-primary col-md-4" [disabled]="newFridgeType.invalid">Add</button>
Sign up to request clarification or add additional context in comments.

Comments

2

You are referencing a todoForm ([disabled]="todoForm.invalid") but your form, as I can see in your code, is named newFridgeType

Comments

0

todoForm is not defined anywhere. So doing .invalid on it causes the error. You need to define what todoform is.

Comments

0

Your form group is named "newFridgeType" so you have to use it in place of "todoForm" in your html (submit button).

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.