I'd like to do a traditional form submit from within a controller. The scenario is that I want to hit a route on my web server and redirect to its response, which I can do with a regular form in HTML, but I also want to do some validation on its fields when the submit button is pressed, and if the validation fails, I don't want to do the route.
I'm aware of ng-valid, but I only want the validation to take place when the button is hit.
Is there a way to conditionally do a form submit from within a controller?
You can add submit method to a FormController. I did so:
<form ng-form-commit action="/" name='payForm' method="post" target="_top">
<input type="hidden" name="currency_code" value="USD">
<button type='button' ng-click='save(payForm)'>buy</button>
</form>
.directive("ngFormCommit", [function(){
return {
require:"form",
link: function($scope, $el, $attr, $form) {
$form.commit = function() {
$el[0].submit();
};
}
};
}])
.controller("AwesomeCtrl", ["$scope", function($scope){
$scope.save = function($form) {
if ($form.$valid) {
$form.commit();
}
};
}])