D
D
Dmitry Medinsky2021-01-11 22:22:19
Angular
Dmitry Medinsky, 2021-01-11 22:22:19

Angular. How to validate input in child component?

Good day!

There is such a problem. It is necessary to validate the form, but the problem is that the inputs are placed in separate components in which the logic necessary for business is hidden. How to validate inputs in child components?

Patent.html

<form [formGroup]="form" (ngSubmit)="submit()">
  <input type="text" name="one" formControlName="one"><br><br>
  <app-childten formControlName="two"></app-childten>
  <button>Send</button>
</form>


parent.ts
form: FormGroup

  ngOnInit() {
    this.form = new FormGroup({
      one: new FormControl('', [Validators.required]),
      two: new FormControl('', [Validators.required]),
    })
  }

  submit() {
    console.log(this.form)
  }


childten.html
<input type="text" name="two" formControlName="two">

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
LastDragon, 2021-01-12
@Dimski

1. https://medium.com/@a.yurich.zuev/angular-nested-t...
2. or explicitly pass `form` to children

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question