0

I came across a situation where I need to call server method before continue with the FORM POST in angularJS. If the server method returns an error I need to display it and remain in the same method which ultimately does not do the POST request.

How can I achieve this with AngularJS?

See my code sample below

HTML

  <form action="http://app.test.com/ProcessTransaction" method="POST">
  <table>
  <tr>
  <td><input type="hidden" name="Amount" value="25.00" /></td>
  </tr>
  <tr>
  <td><input type="hidden" name="Currency" value="NZD" /></td>
  </tr>
  </table>
  <div class="container-fluid align:left"> 
  <button type="submit" class="btn btn-success" ng-disabled="properties.disabled" ng-click="ctrl.action()">{{properties.label}}</button>
  </div>
  </form>

Angular Controller

this.action = function action() {

  var req = {
  method: method,
  url: <some_url>,
  data: angular.copy(data_to_sent),
  params: params
  };

  return $http(req)
  .success(function(data, status) {
  $scope.properties.dataFromSuccess = data;

  })
  .error(function(data, status) {
  $scope.properties.dataFromError = data;

  })
  .finally(function() {

  });
}
4
  • can you tell us on what event you want to call to server before submit ? Commented Sep 27, 2016 at 6:18
  • submit event. When the user presses the sumit button. Commented Sep 27, 2016 at 6:22
  • Okay it's like you want to click submit but dont want to submit unless all validations are passed ! Commented Sep 27, 2016 at 6:25
  • Yes... Unless the server method invoked successfully. Commented Sep 27, 2016 at 6:27

1 Answer 1

1

Okay based on the question OP want's to validate the form and then go for submitting the form values.There are two approaches here

1) Do the form validation on client side using <ng-form> / <form> and required in the input fields. 2) We can skip the traditional way of form submit and just call our submit function on submit click and validate the form from server and if it's success then go for saving the form value or else display errors.

Make below changes for approach (2) as it will be closest for you question.

In HTML

<form>
  <table>
  <tr>
  <td><input type="hidden" name="Amount" value="25.00" /></td>
  </tr>
  <tr>
  <td><input type="hidden" name="Currency" value="NZD" /></td>
  </tr>
  </table>
  <div class="container-fluid align:left"> 
  <button type="submit" class="btn btn-success" ng-disabled="properties.disabled" ng-click="ctrl.action()">{{properties.label}}</button>
  </div>
  </form>

In JS

this.action = function action() {

  var req = {
  method: method,
  url: <some_url>,
  data: angular.copy(data_to_sent),
  params: params
  };

  return $http(req)
  .success(function(data, status) {
  $scope.properties.dataFromSuccess = data;//here data can contain whether the form is valid or invalid...so create a boolean at server if it's valid or not..
    if($scope.properties.dataFromSuccess.isSuccess){
     $http.post(URL,formData).success(function(data){
        $scope.dataSaved = data;
    });
    }else{
       $scope.properties.dataFromError = data; //set the errors in the scope to display
    }

  })
  .error(function(data, status) { //this error means request has been failed to process not the validation error we are checking at server..
  //$scope.properties.dataFromError = data;

  })
  .finally(function() {

  });
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.