如何在模态关闭之前强制进行表单验证

时间:2015-03-15 03:29:22

标签: angularjs twitter-bootstrap bootstrap-modal

我有一个Angular模态形式,我使用Bootstrap设计样式。我想在用户点击“我同意”时验证。" (我不想提交表单,因为它是稍后将提交的更大表单的一部分。)如何在单击按钮时强制进行此验证?此外,表单名称是否应与较大的表单相同或具有自己的名称?

模态:

<form name="agreementForm" novalidate>
    <div class="form-group" ng-class="{
    'has-error':agreementForm.signature.$invalid && !agreementForm.signature.$pristine
    }">

      <label class="control-label" for="signature">Signature</label>
      <input type="text" id="signature" name="signature" ng-model="agreementForm.contact.signature"
             placeholder="Signature (e.g., /John Doe/)" class="form-control" ng-minlength=1 ng-model-options="{ updateOn: 'blur' }" required />

      <span ng-show="agreementForm.signature.$error.required && !agreementForm.signature.$pristine" class="help-block">
               Please type your signature to agree</span>

    </div>

    <div class="modal-footer">
      <button ng-click="$dismiss()" class="btn btn-warning">Cancel</button>
      <button ng-click="$close()" class="btn btn-primary">I agree</button>
  </div>
  </form>

1 个答案:

答案 0 :(得分:0)

ng-submit会解决您的问题吗?例如,

<form ng-submit="$close()">
    <input type="text" ng-model="signature" ng-required="true"/>
    <button type="submit">Submit</button>
</form>

它阻止请求发送到服务器,并且仍应在表单上执行验证。