Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular2 ngNoForm and also do angular form validation

I have a legacy backend server that processes form data as request parameters. We are putting angular2 on the front end. I want to submit the angular2 form so that all fields go as request parameters so that the legacy backend doesn't have to be changed. To do this I have :

<form  ngNoForm action="http://localhost/config/api/v1/angular/submit" target="_blank" method="POST">

But I also want to use the angular2 form validation on the submit button:

<form #f="ngForm" ngNoForm action="http://localhost/config/api/v1/angular/submit" target="_blank" method="POST">
 <button type="submit" [disabled]="!f.form.valid" class="small round">Submit</button>

However this does not work - angular2 complains about having #f="ngForm" when ngNoForm is declared.

Is there any way to be able to do angular2 form validations as usual, and also submit the form as regular request parameters?

like image 764
vanval Avatar asked Sep 29 '16 14:09

vanval


People also ask

What is form validation in Angular?

Form validation is an important part of web application. It is used to validate whether the user input is in correct format or not.

What is Ngnoform in Angular?

It's simply a directive exported from FormsModule which gets automatically added to all <form> tags in your Angular templates once you import the module. Behind the curtains, the ngForm directive creates a top-level FormGroup instance and binds it to your <form> tag to enable you to work with the form.

What is the use of NgForm in Angular?

NgFormlink. Creates a top-level FormGroup instance and binds it to a form to track aggregate form value and validation status.


1 Answers

Force submit using pure JS, this worked for me:

<form ngNoForm [formGroup]="myForm" action="http://test.local/process_post.php" target="_blank" method="POST">
    <input formControlName="alpha" name="alpha"/>
    <input formControlName="beta" name="beta"/>
    <button type="submit" [disabled]="!myForm.valid" onclick="submit()">SEND</button>
</form>
like image 114
Pierre Boissinot Avatar answered Oct 05 '22 13:10

Pierre Boissinot