1

I'm getting the following error when trying to create a form using a custom component.

Error: No value accessor for form control with name: 'nomeEmpresaAerea' Error: No value accessor for form control with name: 'nomeEmpresaAerea' Stack trace: _throwError@webpack-internal:///./node_modules/@angular/forms/esm5/forms.js:2510:11 setUpControl@webpack-internal:///./node_modules/@angular/forms/esm5/forms.js:2380:9 FormGroupDirective.prototype.addControl@webpack-internal:///./node_modules/@angular/forms/esm5/forms.js:6736:9 FormControlName.prototype._setUpControl@webpack-internal:///./node_modules/@angular/forms/esm5/forms.js:7386:45 FormControlName.prototype.ngOnChanges@webpack-internal:///./node_modules/@angular/forms/esm5/forms.js:7299:13 checkAndUpdateDirectiveInline@webpack-internal:///./node_modules/@angular/core/esm5/core.js:12581:9 checkAndUpdateNodeInline@webpack-internal:///./node_modules/@angular/core/esm5/core.js:14109:20 checkAndUpdateNode@webpack-internal:///./node_modules/@angular/core/esm5/core.js:14052:16 debugCheckAndUpdateNode@webpack-internal:///./node_modules/@angular/core/esm5/core.js:14945:55 debugCheckDirectivesFn@webpack-internal:///./node_modules/@angular/core/esm5/core.js:14886:13 View_EmpresaCadastroComponent_0/<@ng:///EmpresaModule/EmpresaCadastroComponent.ngfactory.js:80:5 debugUpdateDirectives@webpack-internal:///./node_modules/@angular/core/esm5/core.js:14871:12 checkAndUpdateView@webpack-internal:///./node_modules/@angular/core/esm5/core.js:14018:5 callViewAction@webpack-internal:///./node_modules/@angular/core/esm5/core.js:14369:21

this is my reactive form:

import { Component, OnInit, Input } from '@angular/core';
import { Router } from '@angular/router';
import { HttpClient } from '@angular/common/http';

import { FormArray, FormBuilder, FormGroup, Validators } from '@angular/forms';

@Component({
  selector: 'app-empresa-cadastro',
  templateUrl: './empresa-cadastro.component.html',
  styleUrls: ['./empresa-cadastro.component.css']
})
export class EmpresaCadastroComponent implements OnInit {

  @Input() empresa = {};

  empresaForm: FormGroup;

  constructor(private route: Router, private http: HttpClient, private fb: FormBuilder) {
    this.createForm();
  }

  createForm() {
    this.empresaForm = this.fb.group({
      nomeEmpresaAerea: '',
      status: 'S',
    });
  }

this is my html:

<form [formGroup]="empresaForm" (ngSubmit)="onSubmit()">
        <div style="margin-bottom: 1em">
          <button type="submit" [disabled]="empresaForm.pristine" class="btn btn-success">Cadastrar</button>
          <button type="button" (click)="limpar()" [disabled]="empresaForm.pristine" class="btn btn-danger">Limpar</button>
        </div>

        <div class="form-group">
          <label class="center-block">Nome:
            <input class="form-control" >
            <app-pf-input-text formControlName="nomeEmpresaAerea" ></app-pf-input-text>
          </label>
        </div>

      </form>

pf-input-text.componente.ts:

import { Component, OnInit, Input } from '@angular/core';

@Component({
  selector: 'app-pf-input-text',
  templateUrl: './pf-input-text.component.html',
  styleUrls: ['./pf-input-text.component.scss']
})
export class PfInputTextComponent implements OnInit {

  @Input() id: string;
  @Input() name: string;
  @Input() value: string;
  @Input() placeholder: string;

  //falta trim

  @Input() maxlength: string;
  @Input() minlength: string;

  @Input() disabled: boolean;
  @Input() required: boolean;

  constructor() { }

  ngOnInit() {
  }

}

pf-input-text.component.html

<div class="input-group">
  <input 
      type="text" 
      id="{{id}}" 
      name="{{name}}" 
      placeholder="{{placeholder}}" 
      attr.maxlength="{{maxlength}}"
      class="form-control"
      disabled="{{disabled}}"
      required="{{required}}">
</div>

1 Answer 1

1

try this in reactive form component

this.empresaForm = new FormGroup({
     'nomeEmpresaAerea': new FormControl(null, Validators.required)});
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.