javascriptformsangular

Angular2: check empty form if radio button is set


I have an Angular2 child component that consist in a radio button and an input text area:

<div class="form-group" *ngIf="message" >
  <input type="text" class="form-control"
  required [(ngModel)]="message.message"
  name="message"/>

  <input type="radio" class="form-control"
   [checked] = "message.default == 1"
   [value] = "message.default"
   (change)="onSelectionChange(message)"
  name="default" >{{message.language}}<br>
</div>

Parent component show than one child component (dynamically) inside a form with a submit button.

Now when I click the submit button, if one of child's text area is empty an error message is showed, but I would that message is showed only if the radio button is checked.

How can I do this?


Solution

  • you could add 1 more condition like this

    <div *ngIf="!message.message && message.default == 1">
      Error here...
    </div>