0

I have troubles understanding how it works... basically when I submit my form, it is validated without checking the required inputs nor anything else.
I've set the 'novalidate' attribute to disable the HTML5 validation, then I've set all my fields as 'required' (same for my mail input with an email validation), and I'm using 'ngMessages' from Angular to check the inputs (is that even necessary??). But I can still submit the form without any field filled in...

What am I doing wrong? Could it come from the fact that I use ng-submit and not ng-click?
Any help is appreciated !

https://jsfiddle.net/WebMoutarde/n1mocvco/

<div ng-controller="MyCtrl">
  <form name="form" novalidate ng-submit="vm.signup()">

      <div class="list">
        <label class="item item-input item-stacked-label noborder">
          <span class="input-label">Nom</span>
          <input type="text" name="nom" ng-model="vm.nom" required>
        </label>
          <hr style="border-style: solid;margin-left: 16px;margin-right: 16px;border-color: #e8e8e8;"/>
        <label class="item item-input item-stacked-label noborder">
          <span class="input-label">Prénom</span>
          <input type="text" name="prenom" ng-model="vm.prenom" required>
        </label>
        <hr style="border-style: solid;margin-left: 16px;margin-right: 16px;border-color: #e8e8e8;"/>
        <label class="item item-input item-stacked-label noborder">
          <span class="input-label">Mail</span>
          <input type="email" name="mail" ng-model="vm.mail" required>
        </label>
        <hr style="border-style: solid;margin-left: 16px;margin-right: 16px;border-color: #e8e8e8;"/>
        <label class="item item-input item-stacked-label noborder">
          <span class="input-label">Mot de passe</span>
          <input type="password" name="password" ng-model="vm.password" required>
        </label>
        <hr style="border-style: solid;margin-left: 16px;margin-right: 16px;border-color: #e8e8e8;"/>
        <label class="item item-input item-stacked-label noborder">
          <span class="input-label">Confirmez votre mot de passe</span>
          <input type="password" name="passwordCheck" ng-model="vm.passwordCheck" required>
        </label>
        <hr style="border-style: solid;margin-left: 16px;margin-right: 16px;border-color: #e8e8e8;"/>

      </div>
          <div ng-messages="form.$error" role="alert">
            <p style="text-align: center;" ng-message="required">You must fill in all fields</p>
            <p style="text-align: center;" ng-message="email">Your email address is invalid</p>
          </div>
          <div class="item noborder">
            <button class="button button-block button-positive" type="submit" >Créer un compte</button>
          </div>
    </form>
  </div>


I've seen many SO questions about this but I still missing something...

DevMoutarde
  • 599
  • 7
  • 21
  • 2
    The Form submit function will execute even if input fields are not valid. You can refer this [answer](http://stackoverflow.com/a/20103586/3764156) to prevent unnecessary form submission. – Midhun Darvin Feb 28 '17 at 10:07
  • Ok thanks ! I did see that question indeed, and yes I think that suits best for my case ! – DevMoutarde Feb 28 '17 at 10:16

1 Answers1

1

You could disabled form submit button till form gets into valid state by using ng-disabled directive on form button

<button class="button button-block button-positive" 
   type="submit" 
   ng-disabled="form.$invalid">
       Créer un compte
</button>

Or even better would be verify form has been valid or ng-submit method and then call your signup method of controller like below.

ng-submit="form.$valid && vm.signup()"
Pankaj Parkar
  • 134,766
  • 23
  • 234
  • 299