3

I am newbie to angular JS and i have created a form which is

HTML

<div data-ng-controller="ReservationController"
    <form class="form-horizontal" role="form">
            <div class="form-group">
              <div class="col-sm-10">
                <label>Guest Name</label>
                <input type="text" class="form-control" placeholder="" ng-model="res_guest_name" required>
              </div>
            </div>

            <div class="form-group">
              <div class="col-sm-10">
                <label>Phone</label>
                <input type="phone" class="form-control"  ng-model="res_member_phone">
              </div>
            </div>
            <div class="form-group">
              <div class="col-sm-10">
                <label>FAX</label>
                <input type="phone" class="form-control"  ng-model="res_member_fax">
              </div>
            </div>
            <div class="form-group">
              <div class="col-sm-10">
                <label>Email</label>
                <input type="email" class="form-control"  ng-model="res_member_email" required>
              </div>
            </div>
          <div class="form-group">
              <div class="col-sm-offset-8 col-sm-10">
                <button type="submit" class="btn btn-success" ng-click="res_save()">Save Changes</button>
              </div>
            </div> 
          </form>
          </div>

CONTROLLER

  function ReservationController($scope, $http,       $cookieStore,$location,$filter) {
     $scope.res_save = function()
      {
       var save_res ="https://pbg.com/save_form.html?contactid="+conId+"&token="+token+"&id="+$scope.resId+"&page=edit&guest_name="+$scope.res_guest_name+"&phone="+$scope.res_member_phone+"&fax="+$scope.res_member_fax+"&email="+$scope.res_member_email;
$http.get(save_res).success(function(response) {
    alert('success');
      });       
      }
      }

My form gets submitted even after the required fields are left empty. it shows the error, then it gets submitted.

3 Answers 3

7

Obviously it will submit the form you didn't handled the submission of form.You just managed the errors :-)

Use ng-disabled="myForm.$invalid" where myForm is the name field on form
<form class="form-horizontal" name="myForm" role="form">

  <button type="submit" class="btn btn-success" ng-disable="myForm.$invalid" ng-click="res_save()">Save Changes</button>

If you don't want the button to be disable till then you can use

<button type="submit" class="btn btn-success" ng-click="res_save(myForm.$valid)">Save Changes</button>

And in controller

   $scope.res_save = function(valid)
         {
           if(valid){
           var save_res ="https://pbg.com/save_form.html?contactid="+conId+"&token="+token+"&id="+$scope.resId+"&page=edit&guest_name="+$scope.res_guest_name+"&phone="+$scope.res_member_phone+"&fax="+$scope.res_member_fax+"&email="+$scope.res_member_email;
    $http.get(save_res).success(function(response) {
        alert('success');
          }); 
        }      
          }
Sign up to request clarification or add additional context in comments.

Comments

0

You are not checking for Form valid state before saving it , don't expect angular to magically stop saving when form is invalid.

Comments

0

You should look into the documentation for ng-submit. If you move the res_save function into an ng-submit on the form (and removed the ng-click on the submit input) it will validate against required fields, prevent execution of the function until they are valid.

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.