AngularJS验证同一页面上的两个表单

时间:2014-10-09 20:23:13

标签: angularjs

我在同一页面上有两个需要验证的表单。事情是其中一种形式是数据绑定,以便我可以检查它是否有效使用$ invalid。但另一种形式似乎没有受到限制,也无法调用它上面的任何方法($ dirty,$ invalid等)

这是我的html页面的片段:

<form name="checkoutShippingForm" role="form" novalidate>
                    <div class="row">
                        <div class="col-md-4">
                            <label for="txtAddress1">Address 1:</label>
                        </div>
                        <div class="col-md-8">
                            <input type="text" name="txtAddress1" data-ng-model="vm.Shipping.Address.AddressLine1" required />
                            <div class="error" data-ng-show="checkoutShippingForm.txtAddress1.$dirty && checkoutShippingForm.txtAddress1.$invalid">
                                <small class="error" data-ng-show="checkoutShippingForm.txtAddress1.$error.required">An Address is required.</small>
                            </div>
                        </div>
                    </div>
                    Invalid? {{checkoutShippingForm.$invalid}}  <!--Doesn't show -->
</form>

<form name="checkoutBillingForm" role="form" novalidate>
                    <div class="row">
                    <div class="col-md-4">
                        <label for="txtBillingAddress1">Billing Address 1:</label>
                    </div>
                    <div class="col-md-8">
                        <input type="text" id="txtBillingAddress1" name="txtBillingAddress1" data-ng-model="vm.Billing.Address.AddressLine1" required />
                        <div class="error" data-ng-show="checkoutBillingForm.txtBillingAddress1.$dirty && checkoutBillingForm.txtBillingAddress1.$invalid">
                            <small class="error" data-ng-show="checkoutBillingForm.txtBillingAddress1.$error.required">An Address is required.</small>
                        </div>
                    </div>
                </div>
                    invalid: {{checkoutBillingForm.$invalid}}  <!--Shows true -->
</form>

2 个答案:

答案 0 :(得分:1)

请参阅下面的

替换

   <div class="error" data-ng-show="checkoutBillingForm.txtBillingAddress1.$dirty && checkoutBillingForm.txtBillingAddress1.$invalid">

   <div class="error" data-ng-show="checkoutBillingForm.txtBillingAddress1.$dirty || checkoutBillingForm.txtBillingAddress1.$invalid">

var app = angular.module('app', []);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app">
  <form name="checkoutShippingForm" role="form" novalidate="">
    <div class="row">
      <div class="col-md-4">
        <label for="txtAddress1">Address 1:</label>
      </div>
      <div class="col-md-8">
        <input type="text" name="txtAddress1" data-ng-model="vm.Shipping.Address.AddressLine1" required="" />
        <div class="error" data-ng-show="checkoutShippingForm.txtAddress1.$dirty && checkoutShippingForm.txtAddress1.$invalid">
          <small class="error" data-ng-show="checkoutShippingForm.txtAddress1.$error.required">An Address is required.</small>
        </div>
      </div>
    </div>
    Invalid? {{checkoutShippingForm.$invalid}}
    <!--Doesn't show -->
  </form>
  <form name="checkoutBillingForm" role="form" novalidate="">
    <div class="row">
      <div class="col-md-4">
        <label for="txtBillingAddress1">Billing Address 1:</label>
      </div>
      <div class="col-md-8">
        <input type="text" id="txtBillingAddress1" name="txtBillingAddress1" data-ng-model="vm.Billing.Address.AddressLine1" required="" />
        <div class="error" data-ng-show="checkoutBillingForm.txtBillingAddress1.$dirty || checkoutBillingForm.txtBillingAddress1.$invalid">
          <small class="error" data-ng-show="checkoutBillingForm.txtBillingAddress1.$error.required">An Address is required.</small>
        </div>
      </div>
    </div>
  </form>
</div>

答案 1 :(得分:0)

您可以在一个页面中多次使用ng-form。

HTML:

<ng-form name="scope.form">
            <div>
                <table>
                    <tbody>
                        <tr>
                            <td class="col-md-3">
                                <div class="label-color">HEADER NAME <span class="red"><strong>*</strong></span></div>
                            </td>
                            <td class="col-md-9">
                                <input type="text" name="headerName" id="headerName" ng-change="scope.checkHeaderName(header.headerName)"
                                    ng-model="header.headerName" maxlength="50" class="form-control" required>
                                <div ng-show="scope.form.$submitted || scope.form.headerName.$touched">
                                    <span ng-show="scope.form.headerName.$error.required" class="label-color validation-message">Header Name is required</span>                                        
                                </div>
                            </td>
                        </tr>                            
                        <tr>
                            <td class="col-md-3">
                                <div class="label-color">HEADER DESCRIPTION <span class="red"><strong>*</strong></span></div>
                            </td>
                            <td class="col-md-9">
                                <textarea name="headerDesc" id="headerDesc" ng-model="header.headerDesc" class="form-control" maxlength="50" required rows="3" cols="15"></textarea>
                                <div ng-show="scope.form.$submitted || scope.form.headerDesc.$touched">
                                    <span ng-show="scope.form.headerDesc.$error.required" class="label-color validation-message">Header description is required</span>
                                </div>
                            </td>
                        </tr>
                    </tbody>
                </table>                    
            </div>
        <button ng-click="scope.changePage(2)" type="button" class="btn btn-default pull-right">Next</button>
        </ng-form>


        <ng-form name="scope.supervisionform">               
            <div class="supervision-search-block">                                                
                <div class="input-group">
                    <div class="col-lg-6 col-md-6 col-sm-6 col-xs-12">
                        <div class="form-group">
                            <div class="label-color">CERTIFICATE NUMBER (LAST 6 DIGITS) <span class="red"><strong>*</strong></span></div>
                            <input type="text" maxlength="6" pattern="[0-9]{6}" name="licenseNumber" id="licenseNumber" class="form-control" ng-model="scope.licenseNumber" required />
                            <span ng-show="(scope.supervisionform.licenseNumber.$dirty && scope.supervisionform.licenseNumber.$error.required)" class="help-block">License Number is required</span>
                            <span ng-show="scope.supervisionform.licenseNumber.$dirty && scope.supervisionform.licenseNumber.$error.pattern" class="help-block">Please provide only 6 digits</span>
                        </div>
                    </div>
                    <div class="col-lg-6 col-md-6 col-sm-6 col-xs-12">
                        <div class="form-group">
                            <div class="label-color">LAST NAME <span class="red"><strong>*</strong></span></div>
                            <input type="text" maxlength="20" name="lastName" id="lastName" class="form-control" ng-model="scope.lastName" required/>
                            <span ng-show="scope.supervisionform.lastName.$dirty && scope.supervisionform.lastName.$error.required" class="help-block">Last Name is required</span>
                        </div>
                    </div>
                    <span class="input-group-btn">                                
                        <button type="button"                                             
                            ng-click="scope.validateSupervisionCertificate()" 
                            class="btn btn-primary">Search
                        </button>                            
                    </span>
                </div>                                        
            </div>
            <div>                    
                <button ng-click="scope.submitPlantReg();" type="button" class="btn btn-default pull-right">Submit</button>
            </div>
        </ng-form>

在控制器中:

changePage(pageNumber) {
    let self = this;

    if (self.form && self.form.headerName.$invalid) {
        self.form.premise.$setDirty();
        self.form.premise.$setTouched();
        return;
    }

    if (pageNumber == 2) {
        Do something..
    }        
}

submitPlantReg() {
    let self = this;

    // Supervision verification form validation
    if (scope.supervisionForm.supervisionType.$invalid || scope.supervisionForm.licenseNumber.$invalid || scope.supervisionForm.lastName.$invalid ||
        scope.supervisionForm.contractorName.$invalid || scope.supervisionForm.contractorClass.$invalid) {
        scope.supervisionForm.$setDirty();
        scope.supervisionForm.licenseNumber.$setDirty();
        scope.supervisionForm.lastName.$setDirty();
        scope.supervisionForm.contractorName.$setDirty();
        scope.supervisionForm.contractorClass.$setDirty();
        scope.supervisionForm.$setSubmitted();
        return;
    }

    Do something else...
}