2
votes

I am using Template Driven Form.

Parent Component HTML

<form #BasicForm="ngForm" (ngSubmit)="onBasicDetailsSubmit()" id="BasicForm">

  <app-input-text [(sharedVar)]="dashboardDetails.Text1" [isMandatory]="true" ></app-input-text>
  <app-input-text [(sharedVar)]="dashboardDetails.Text2" [isMandatory]="false"></app-input-text>

  <input type="submit" value="Save" [disabled]="!BasicForm.valid" class="btn btn-success">

</form>

Child Component

TS

@Input() sharedVar: number;
@Input() isMandatory: boolean;

@Output() sharedVarChange = new EventEmitter();


change(newValue) {
  this.sharedVar = newValue;
  this.sharedVarChange.emit(newValue);
}

HTML

<input type="text" class="form-control" [(ngModel)]="sharedVar" (ngModelChange)="change($event)" [attr.required]="isMandatory">

The Submit button is not getting disabled. I have tried writing required in child component as well as parent component selector, but it doesn't work. Please help.

3
In parent component html you aren't accessing sharedVarChange no?Malindu Sandaruwan
No. I am trying it nowAdrita Sharma
You could rather share the same form between parent and child. stackoverflow.com/questions/51281527/… Only difference is you have template driven forms. That should not be an issue. Once done, you can fetch form controls to validate in either of the componentsAmit Chigadani

3 Answers

2
votes

As you are using Template-Driven-Form, the best way to validate Child components is to create a custom-Directivelike this that you will always add in each field that you want to validate in the Child-Component-Form:

You can use this one:

import {Directive, OnInit} from '@angular/core';
import {NgControl, NgForm, NgModel} from "@angular/forms";

/**
 * This attribute directive must be used to each input-field of a childComponent.
 * That input-field must contain a NgModel attribute, else the application must throw an error
 * Usage: (<input class="form-control" type="text" registerChildComponentToForm
 *          [(ngModel)]="testname" name="testname" required />
 */

@Directive({
    selector: '[registerChildComponentToForm]',
    providers: [NgModel]
})
export class RegisterTemplateFormModelDirective implements OnInit {

    constructor(private form: NgForm, private eltControl: NgControl) {
    }

    ngOnInit() {
        if (this.form && this.eltControl) {
            this.form.form.addControl(this.eltControl.name, this.eltControl.control);
        }
    }

}

Then register it into declarations and exports in your App-Module

declarations: [
        RegisterTemplateFormModelDirective,
        ...
],
exports: [
        RegisterTemplateFormModelDirective,
        ...
]

Suppose that your <app-input-text> is this HTML code, then you should just use the directive(registerChildComponentToForm) like this:

<input id="iban" name="iban" [(ngModel)]="bank.iban" #ibanRef="ngModel" 
  [required]="isMandatory" registerChildComponentToForm/>
0
votes

Parent Component HTML

<app-input-text [(sharedVar)]="dashboardDetails.Text1" (sharedVarChange)="sharedVarChangeHandle($event)" ...

Parent Component TS

sharedVarChangeHandle($event) {
    // by evaluating the value passed you can update a variable (ex: disableSubmit)
} 

Parent Component HTML

<input type="submit" value="Save" [disabled]="!disableSubmit" class="bt .....
0
votes

Child Component ts

valid // this is a public variable

change(newValue) {
  this.sharedVar = newValue;
  this.sharedVarChange.emit(newValue);
  // here conditioanally update the **valid** variable (true or false)
}