0

So I have one form and I need two submit buttons, i need the ngNativeValidate to validate it, how can I get the value of the button that submitted the form in the onClickResolveTramite()?

It looks simples but I've searched for it and found nothing, i just need a true or false to know if it's approved or not.

<div class="modal-body">

  <form (ngSubmit)="onClickResolveTramite(form)"
        #form="ngForm"
        id="resolveTraimte"
        ngNativeValidate
        autocomplete="off">

    <!-- STATUS -->
    <div class="form-row">
      <div class="form-group col">
        <label for="message">
          Reason
        </label>
        <span>*</span>
        <textarea class="form-control"
                  name="message"
                  rows="4"
                  id="message"
                  [(ngModel)]="tramite.message"
                  placeholder="Reason"
                  required></textarea>
      </div>
    </div>


    <div class="form-row">
      <div class="form-group col">
        <label for="user">
          User
          <span>*</span>
        </label>
        <select class="form-control"
                name="user"
                id="user"
                [(ngModel)]="user"
                required>
          <option *ngFor="let user of tramite.users"
                  [ngValue]="user">
            {{user.nome}}
          </option>
        </select>
      </div>
    </div>

  </form>
</div>

<div class="modal-footer"
     style="border: none;">
  <button class="btn btn-light"
          (click)="onClickclose()">
    Cancel
  </button>
  <button *ngIf="tramite.needApro"
          type="submit"
          form="resolveTraimte"
          class="btn btn-danger"
          name="submitTramite"
          value="repprove"
          formaction="repprove">
    Repprove
  </button>
  <button class="btn btn-success"
          type="submit"
          name="submitTramite"
          form="resolveTraimte"
          value="approve"
          formaction="approve">
    {{ tramite.needApro ? 'Approve' : 'Complete' }}
  </button>

//  Controller

onClickResolveTramite(form: NgForm) {
    console.log(form);
  }
  • 1
    You can’t have two submit buttons for one form, why not just use a click event? You have to set the type on all buttons inside the form to button, if not it’ll trigger a submit. Pass the boolean as the argument. – alphapilgrim Aug 02 '18 at 13:00
  • Highly related: https://stackoverflow.com/questions/932653/how-to-prevent-buttons-from-submitting-forms – hellow Aug 02 '18 at 13:09
  • as I said i need the ngNativeValidate to validate the form, so with a click event iI wont have the validation – Felipe Zardo Aug 02 '18 at 18:04

1 Answers1

1

Assign different id to each button. Then you can obtain id of button which triggered submit using document.activeElement.id

Wilhelm Olejnik
  • 2,382
  • 3
  • 14
  • 21