1

Hi I have a form in one component (without button) and I have a parent component with a button to go to the next step. I would like to know how to check if the form is valid in the parent component to be able to go to the next step.

childcomponent.html

<form
  
  [formGroup]="form"
  class="clr-row"
  (ngSubmit)="formSubmit.emit()"
>
  <select
      formControlName="chosenYear"
      required
      name="years"
      (click)="changeYearComponent()"
      [(ngModel)]="chosenYear"
    >
      <option *ngFor="let year of years" [value]="year">
        {{ year }}
      </option>
    </select>
</form>

parentcomponent.ts

@ViewChild(childComponent)
  public childComponent!:childComponent;
@Input() public form: FormGroup;

 test()  {
    if (this.form.valid) {
      this.next();
     }
}

parentcomponent.html

<button
            type="submit"
            class="btn btn-primary"
            (click)="test()"
           
          >
            NEXT
          </button>

1 Answer 1

3

You can use view child to see your child component

@ViewChild(ChildComponent)
yourChildComponent!: ChildComponent;

then you could access to child properties and methods.

nextStep(){
 if(this.yourChildComponent.form.valid){
  ...
 }
}
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.