2

I have a simple form like this. I am submitting this form via ajax using angularjs. I want to validate this form before sending ajax request and need to show the error to user. I am a newbie to angular. Please Help me

Here is my code.

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.7/angular.js"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.7/angular-route.js"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.7/angular-messages.js"></script>
<div ng-app="Myapp">
    <div ng-controller="orderFormController">

        Item :   <input type="text" name="item"  ng-model='item' required> <p></p>
     Rate:   <input type="text" name="rate"  ng-model='rate' required> <p></p>

        <button type="button"  ng-click='saveorder()' >SAVE ORDER</button>
    </div>    
    <script>
        var Myapp = angular.module('Myapp', ["ngRoute"]);
        Myapp.controller('orderFormController', ['$scope', '$http', function ($scope, $http) {


            $scope.saveorder = function () {

                 // Validate form here and set the error in form

                 $http.post("order/save/", data).success(function (res, status, headers, config) {
                     $scope.message = res;

                }).error(function (data, status) { 
                    $scope.message = res;
                });
            }

        }]);
    </script>             

3 Answers 3

1

The very simple implementation could be done in a few steps:

  1. Use <form> tag with a name attribute. Use saveorder in ng-submit attribute.
  2. Create errors containers, and make their visibility to depend on errors: <span class="error" ng-show="form.item.$error.required">

Final result should look like:

<div ng-controller="orderFormController">    
    <form name="form" ng-submit="saveorder()">
        Item :   <input type="text" name="item"  ng-model='item' required> <p></p>
        <span class="error" ng-show="form.item.$error.required"> Item field is required!</span>
        Rate:   <input type="text" name="rate"  ng-model='rate' required> <p></p>    
        <span class="error" ng-show="form.rate.$error.required"> Rate field is required!</span>
        <button type="button">SAVE ORDER</button>
    </form>    
</div>  

You can read Angular native documentation about form validation. More details are provided there.

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

Comments

0

Read this article.. https://scotch.io/tutorials/angularjs-form-validation

It explained it really well and and if still there are confusions , ask about those after putting some effort.

Comments

0

you should use angular form validation for that or for custom validation use

   var Myapp = angular.module('Myapp', ["ngRoute"]);
   Myapp.controller('orderFormController', ['$scope', '$http', function($scope, $http) {
   $scope.formdata = {};

   $scope.saveorder = function(formdata) {
       $scope.error = '';

       // Validate form here and set the error in form
       if (angular.isUndefined(formdata.item) == true || angular.isUndefined(formdata.rate) == true) {
           $scope.error = "Both Item and Rate are required."
           return false;
       } else {
           $scope.error = '';
       }
       $http.post("order/save/", formdata).success(function(res, status, headers, config) {
           $scope.message = res;

       }).error(function(status, res) {
           $scope.message = res;
       });
   }

   }]);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.7/angular.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.7/angular-route.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.7/angular-messages.js"></script>
<div ng-app="Myapp">
    <div ng-controller="orderFormController">
<span style="color:red">{{error}}</span><br>
        
        Item :   <input type="text" name="item"  ng-model='formdata.item' required> <p></p>
     Rate:   <input type="text" name="rate"  ng-model='formdata.rate' required> <p></p>

        <button type="button"  ng-click='saveorder(formdata)' >SAVE ORDER</button>
    </div>   

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.