Form gets submitted even if the required fields ar

2019-07-18 13:49发布

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条回答
Fickle 薄情
2楼-- · 2019-07-18 14:16

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

查看更多
疯言疯语
3楼-- · 2019-07-18 14:29

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');
          }); 
        }      
          }
查看更多
萌系小妹纸
4楼-- · 2019-07-18 14:30

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.

查看更多
登录 后发表回答