【发布时间】:2014-07-02 10:27:39
【问题描述】:
关于 SO 有很多类似的问题。我已经经历了很多,但没有发现我的问题。
我有一个使用ng-submit 的表单。不幸的是,即使表单无效,按 Enter 或单击提交也有效。调用ng-submit 方法。
CodePen 示例:http://codepen.io/calendee/pen/tgFhq
<form name="testForm" novalidate ng-submit="submit()">
<label for="firstname">First Name:</label>
<input type="text" name="firstname" ng-model="data.firstname" required="true" ng-minlength="4" ng-maxlength="10">
<div class="errors">
<p>firstname Errors:</p>
<p ng-show="testForm.firstname.$error.required">firstname is required</p>
<p ng-show="testForm.firstname.$error.minlength">firstname is too short</p>
<p ng-show="testForm.firstname.$error.maxlength">firstname is too long</p>
</div>
<label for="lastname">Last Name:</label>
<input type="text" name="lastname" ng-model="data.lastname" ng-required="true" ng-minlength="4" ng-maxlength="10">
<div class="errors">
<p>lastname Errors:</p>
<p ng-show="testForm.lastname.$error.required">lastname is required</p>
<p ng-show="testForm.lastname.$error.minlength">lastname is too short</p>
<p ng-show="testForm.lastname.$error.maxlength">lastname is too long</p>
</div>
<button class="button button-balanced" type="submit">Submit</button>
</form>
如果我更改表单ng-form,即使表单有效,提交事件也根本不起作用。
CodePen 示例:http://codepen.io/calendee/pen/imJdc
<ng-form name="testForm" novalidate ng-submit="submit()">
<label for="firstname">First Name:</label>
<input type="text" name="firstname" ng-model="data.firstname" required="true" ng-minlength="4" ng-maxlength="10">
<div class="errors">
<p>firstname Errors:</p>
<p ng-show="testForm.firstname.$error.required">firstname is required</p>
<p ng-show="testForm.firstname.$error.minlength">firstname is too short</p>
<p ng-show="testForm.firstname.$error.maxlength">firstname is too long</p>
</div>
<label for="lastname">Last Name:</label>
<input type="text" name="lastname" ng-model="data.lastname" ng-required="true" ng-minlength="4" ng-maxlength="10">
<div class="errors">
<p>lastname Errors:</p>
<p ng-show="testForm.lastname.$error.required">lastname is required</p>
<p ng-show="testForm.lastname.$error.minlength">lastname is too short</p>
<p ng-show="testForm.lastname.$error.maxlength">lastname is too long</p>
</div>
<button class="button button-balanced" type="submit">Submit</button>
</ng-form>
有人对我在这里做错了什么有建议吗?
【问题讨论】:
-
我的开源模块解决了这个问题,它也让你的 html 更干净 :-) jonsamwell.github.io/angular-auto-validate
标签: forms angularjs validation