Ng-submit does not prevent sending

There are many similar questions on SO. I went through many of them and did not find my problem.

I have a form using ng-submit. Unfortunately, pressing the Enter or Submit button is performed even if the form is invalid. The method is called ng-submit.

CodePen example: 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>

If I change the form ng-form, the submit event does not work at all, even if the form is valid.

CodePen example: 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>

Does anyone have a suggestion on what I'm doing wrong here?

+4
source share
2

$valid:

<form name="testForm" novalidate ng-submit="testForm.$valid && submit()">

submit , testForm.$valid .

: https://www.codeschool.com/courses/shaping-up-with-angular-js.

+7

ng-submit, . submit() , . , .

+1

Source: https://habr.com/ru/post/1546830/


All Articles