Angular onsubmit for form - angularjs

I need the functionality of the onsubmit attribute on a form - namely when I call this onsubmit function, it must return truthy in order for the form to post. However I would like to do this with an angular function call, along the lines of:
<form id="form-submit-canvas" autocomplete="on" enctype="multipart/form-data" accept-charset="UTF-8" method="POST" onsubmit="{{FormSubmit.validate()}}" action="{{FormSubmit.SUBMIT_URL}}" novalidate>
However the above gives out errors about interpolation being used on onsubmit. I tried putting in ng-submit and it does not work since the action property I have set is overridden.

Not sure if it's still valid for you, but as I've been fighting the similar problem.
Following answer brought me to the right track: https://stackoverflow.com/a/17769240/1581069
So, to sum up the steps required:
you should call pure javascript (angular-free one) in your onsubmit, like (please note: if you return false there => form won't be submited):
<form id="form-submit-canvas" ... name="fooForm" onsubmit="return externalValidate();" ...
And implement the the method to hand-over to angular, like:
function externalValidate(){
return angular.element($("#form-submit-canvas")).scope().validate();
}
where your validate() method should be just one you referred to previously (implemented in your angular controller).
and externalValidate() should be implemented outside of any angular-specific component.
Moreover make sure sure to return correct result from the validate() method (to prevent form submission). Maybe something like:
$scope.validate = function () {
// TODO implement whatever magic you need prior to submission
return $scope.fooForm.$valid;
}

Try using the ng-submit attribute instead of onsubmit. Related advise: Many attributes are not supported by Angular. You should find the ng- equivalent of the attribute you want to use to get Angular to react to them properly.
Example:
<form id="form-submit-canvas" autocomplete="on" enctype="multipart/form-data" accept-charset="UTF-8" method="POST" ng-submit="validate()" novalidate>
...
<input type="submit" name="submit" value="Submit" />
</form>
Where the validate() method in ng-submit is a method declared on your $scope.
Here is a related tutorial: AngularJS Tutorial (ng-submit)

Well I know this could help somebody looking for how to use the submit form action in Angular. I'm working with Angular 13 and this is how we can capture the submit action:
<form [formGroup]="formData" (onSubmit)="onSubmit()">
<button type="submit">Click here!</button>
</form>
The reference is here

Related

Prevent Angular form submit when invalid AND using a form action URL

I have a form with a URL action set. When form.$invalid is true (or form.$valid is false) I want to make the form NOT submit to the URL and show the invalid form errors in the UI.
In my particular case here, I'm using a framework that provides an Angular Controller for me, where I do not have the ability to add/modify functions on it! I need to be able to do this only by making changes to the template.
Here's what I have so far (simplified)
<form novalidate
name="form"
method="post"
ng-attr-action="{{model.loginUrl}}"
ng-submit="return form.$valid">
<div class="form-group" ng-class="{'has-danger' : form.$submitted && form.username.$invalid}">
<input type="text"
name="username"
required
class="form-control"
ng-model="model.username">
<div class="form-control-feedback" ng-if="form.$submitted && form.username.$invalid">
A username is required!
</div>
</div>
<button type="submit">Sign In</button>
</form>
A lot of what I've seen online says to do something like
ng-submit="form.$valid && model.myCustomSubmitFn()"
but that doesn't use a URL form action, and I do not have the ability to add a custom function with this framework
Angular passes the $event in the context of ng-submit. So you call $event.preventDefault(). Your ng-submit would change to
<form novalidate
name="form"
ng-attr-action="{{model.loginUrl}}"
ng-submit="form.$invalid && $event.preventDefault();form.$submitted=true;"
>
See plunker.
THE BELOW IS AN ANSWER THAT WILL NOT WORK FOR THIS QUESTION-- I AM ONLY LEAVING IT HERE SO NO ONE ELSE TRIES THIS AS THE SOLUTION
Might ngDisabled work for you in this case?
<button type="submit" ng-disabled="form.$invalid">Sign In</button>
This is certainly a tricky situation since you only have access to edit the template. I'm not seeing an "angular" way to solve your problem with being able to edit the controller. Luckily though, this can be solved in the template alone with some vanilla JavaScript.
Try adding an onclick event handler to your button that prevents form submission if your form's input(s) do not match your validation criteria.
<button type="submit" onclick="if(form.username.value.length < 1) { event.preventDefault(); }">Sign In</button>
I'm not incredibly proud of this solution and it will certainly get dirty if you have more than one input in your form, however it looks like the only work-around for your unfortunate situation.
Reactive Forms
If you wanted to declair this in the HTML of your reactive form you can simply do something like this:...
<form [formGroup]="formGroupVar" (ngSubmit)="formGroupVar.valid ?
submitFunc() : null" novalidate>...</form>
So what you are doing is making your (ngSubmit) a ternary operator, the comparison would be your formGroupVar.valid, when true, you would execute your submitFunc() else null.
NOTE: There is no reason why you wouldn't just do this in your TypeScript file, it would make sense to keep your HTML as 'clean' as possible, but that doesn't mean it is not possible!

Laravel HTML Facade conflict with Angular ng-submit

I know that while using Angular's ng-submit will disable the default form action and use angular's function but the problem is when it is used with Laravel blade templating it somehow do not disable the function... So as below code, if i do something like this
{!! Form::open(array('class' => 'form-horizontal signup-form', 'ng-submit' => 'updateProfile()')) !!}
and when I inspect the generated code, it gives me something like this
<form method="POST" action="http://www.example.com:8000/profile/update" accept-charset="UTF-8" class="form-horizontal signup-form ng-pristine ng-valid" ng-submit="updateProfile()"><input name="_token" type="hidden" value="8pNEBRXkUSfGUvSWfvFAmFPULLBO6kiet6LieC5B">
which when the submit button is triggered, it executes the form's action instead of calling the Angular's thus giving me an MethodNotAllowedException. But when I use manual coding like this
<form ng-submit="updateProfile()" class="form-horizontal signup-form">
everything works like a charm. I wonder if anyone knows any workaround to this issue can share their experience with me. i know that i can do manual code just for these section but again, I want to maintain consistency in the codes.
I think when you submit the form which generate by laravel form builder, it also calling the updateProfile() function and may be you don't prevent the default action of the event at the end, because of that form has a action so that page will submit to the action. If your form has no action then there is no way to submit the form as like your manual code, if you put a action to the manually coded form with the same updateProfile() it will submit the form to the action you provided.
to avoid that put event.preventDefault(); inside the updateProfile() function.
$scope.updateProfile = function(event) {
......
event.preventDefault();
}
and pass the event to the function as, updateProfile($event)
{!! Form::open(array('class' => 'form-horizontal signup-form', 'ng-submit' => 'updateProfile($event)')) !!}

AngularJS Disabling Forms When Controllers are Created

AngularJS disables (cannot submit) forms that do not have a specific action set:
<form action="">
when you create a controller. It does not have this issue when creating directive or factories.
You can see this in a plunk here:
http://plnkr.co/edit/gWFRMKGO3FzZtOgs4VmW?p=preview
Form is defined as:
<form action="" method="post">
If you delete the starting on line 6, you will be able to submit the form.
A simple solution is to define the action, but I'd rather not do this, as it is not necessary.
UPDATE
Some details can be found here on trying to get this change in Angular:
https://github.com/angular/angular.js/pull/3776
You can use ng-submit to handle that.
<form ng-submit="submitForm()" method="post">
<input name="test" value="11111111" />
<button type="submit" name="submit" value="1">Send</button>
<input type="submit" name="submit2" value="Send2" />
</form>
That way the form will submit normally and you must actually send the data on your submitForm function (just an example name).
Here is a quick plnkr: http://plnkr.co/edit/lwWVG0CDHSGMtMU0B8Nj?p=preview
Notice that you can submit using the buttons and also by pressing enter on the field. I hope that's what you've been asking for.
Thanks

Submit a form directly to controller

What is the simplest way to submit a form directly to a controller?
I have a form like:
<form name="searchForm">
<input type="text" name="query" ng-model="query">
<button type="submit">Search</button>
</form>
And a controller that handles routes for #/search?query=xxxx.
How can I make it so when the form is submitted it's equivalent to a user clicking a link with a href="#/search?query=xxxx"? I would think there would be something like ng-action="#/search" but I haven't found anything that simple.
Please see below points to solve your problem.
Use ng-submit="SaveData(pass your model value here)" as attribute of form tag.
Create SaveData scope method in your controller JS.
Create URL and use window.location to redirect.

AngularJS post form to external URL

I have the following form in my AngularJS app which contain hidden fields with values filled based on user selection on some inputs on the form (radio buttons...etc), when the user click on the Submit link I should route the user to an external URL while passing hidden fields just as any normal form submission. Unfortunately I can't do this as some of the hidden field values are dependent on some calculations inside a function of the view related controller (as shown below in controller code, so I was wondering is there a way I can call the controller function from this form, then the controller function post the whole form and its field? Any example is highly appreciated. Thanks.
Note I am using link instead of a button.
<form name="clientPaymentForm" id="clientPaymentForm" action="https://www.paypal.com/cgi-bin/webscr" method="post" target="_top">>
<div>
<fieldset>
<input id="name" type="text" required placeholder="Client Name" ng-model="client.name">
...
...
<input type="hidden" name="amount" ng-value="order.total">
...
...
<a class="orderButton" href="javascript:{}" onclick="document.getElementById('clientPaymentForm').submit(); return false;">Order Now</a>
</fieldset>
</div>
</form>
Controller:
$scope.processOrder = function(){
//Order calculation happens here to update order.total value and can only happen after click click Order Now to place the order...
};
I guess this is a bit late, but what you want to use is the ng-click directive which will allow you to call functions defined directly on the scope.
Assuming that you've defined $scope.processOrder, change your a tag to the following:
<a class="orderButton" ng-click="processOrder()">Order Now</a>
And everything should work as hoped.
Alternatively, you could use ng-submit on the form to have it work when you press the "Enter" or "Return" key, as in:
<form name="clientPaymentForm" id="clientPaymentForm" action="https://www.paypal.com/cgi-bin/webscr" method="post" target="_top" ng-click="processOrder()">.

Resources