I have a reactive form inside a component, like this:
<form [formGroup]="edit">
<div class="form-group row">
<label for="{{componentId}}_iptItemName" class="col-4 col-form-label">Name</label>
<div class="col-8">
<input id="{{componentId}}_iptItemName" type="text" class="form-control"
formControlName="name"/>
</div>
</div>
<!-- more form groups -->
</form>
Most of the fields look the same, so I would like to take the div.form-group and make a reusable component. The problem is, I can't get the formControlName into the nested component.
When I try just specifying formControlName on the new component, I get:
No value accessor for form control with name: 'name'
When I use an @Input() appFormControlName: string and pass that value to the input's [formControlName], I get:
formControlName must be used with a parent formGroup directive. You'll want to add a formGroup directive and pass it an existing FormGroup instance (you can create one in your class).
I've seen things where I could create an extra form group for each nested component, or register some kind of custom ControlValueAccessor to make it a "true" form control, but both of those seem overly complicated for what I'm doing. I just want to create a component to keep things DRY, not add complexity by creating custom form controls or kludge in single-value form groups.
Am I just missing something simple?