I'd like to transfer my ngForm and ngModelGroup from parent component to child component and also use form validation functionality in the child component.
Parent component.html:
<md-step [stepControl]="createIntegrationPartnerRequestGroup">
<form #createIntegrationPartnerRequestForm="ngForm">
<div ngModelGroup="createIntegrationPartnerRequestGroup" #createIntegrationPartnerRequestGroup="ngModelGroup">
<div class="row p-row-padding">
<div class="col-xs-12">
<ng-template mdStepLabel>
{{'INTEGRATION_PARTNER_REQUEST' | translate}}
</ng-template>
</div>
</div>
<integration-partner-request [form]="createIntegrationPartnerRequestForm"
[model]="createIntegrationPartnerRequestGroup"></integration-partner-request>
</div>
</form>
</md-step>
Child Component.html (excerpt):
<div class="row p-row-padding">
<div class="col-xs-6">
<md-input-container class="p-full-width">
<input mdInput
ngModel name="name"
#name="ngModel"
required
(change)="showForm()"
placeholder="{{'WELCOME_WIZARD.PARTNER_NAME' | translate}}">
<md-error *ngIf="name.touched && name.invalid">
<span *ngIf="name.errors.required">
{{'WELCOME_WIZARD.FIELD_REQUIRED' | translate}}
</span>
<span *ngIf="name.errors.pattern">
{{'WELCOME_WIZARD.INVALID_FIELD_FORMAT' | translate}}
</span>
</md-error>
</md-input-container>
</div>
<div class="col-xs-6">
<md-input-container class="p-full-width">
<input mdInput
ngModel name="status"
#status="ngModel"
placeholder="{{'WELCOME_WIZARD.PARTNER_STATUS' | translate}}">
<md-error *ngIf="status.touched && status.invalid">
<span *ngIf="status.errors.required">
{{'WELCOME_WIZARD.FIELD_REQUIRED' | translate}}
</span>
<span *ngIf="status.errors.pattern">
{{'WELCOME_WIZARD.INVALID_FIELD_FORMAT' | translate}}
</span>
</md-error>
</md-input-container>
</div>
</div>
Child Component.ts:
export class ChildComponent implements OnInit {
@Input() public form: FormGroup;
@Input() public model: NgModelGroup;
@Input() public type: string;
public formResult;
public modelResult;
constructor() {
}
ngOnInit() {
this.formResult = this.form;
this.modelResult = this.model;
}
public showForm() {
console.log(this.formResult);
console.log(this.modelResult);
}
}
There's also a button in the child component which should only work if the required field is not empty, but that is not the case, since the modelResult
doesn't have any field values...:
<button
class="p-full-width"
md-raised-button
[disabled]="formResult.invalid"
mdStepperNext>
{{'WELCOME_WIZARD.NEXT' | translate}}
</button>
What am I doing wrong?