2

how to prevent the auto validation on a form? Everytime I load the template with the form, it automatically validates itself even without any data typed in to the inputs.
I want to trigger the validation on submit.

HTML:

<span ng-show='message.$invalid == true'>This e-mail is black-listed!</span>
    <form novalidate class="dealermessageform" name="message">
        <input type="text" name="messagename" placeholder="Name" class="messagename" ng-model="user.name" required>
        <input type="email" name="messageemail" placeholder="E-Mail Adresse*" class="messageemail" ng-model="user.email" required>
        <textarea class="message" name="message" placeholder="Schreibe eine Nachricht. *" required></textarea>
        <span class="help-block">*alle markierten Felder sind Pflichtfelder</span>
        <div class="grid_4 alpha omega submitbutton">
            <input type="submit" class="submitbutton" value="Senden" id="submit" ng-click="send()">
        </div>
    </form>  

And how is it possible, to show the "span" field above the form, ONLY if some errors are TRUE after submit?
It also directly shows up when I enter the form.

Marek123
  • 1,193
  • 7
  • 35
  • 75

1 Answers1

1

It shouldn't be a problem if validation already occurs. You just shouldn't show the error message. You can do that with $pristine or $dirty, depending if you use ng-show or ng-hide:

<span class="help-block" ng-show="message.$dirty && message.$invalid">*alle markierten Felder sind Pflichtfelder</span>

In your send() function, you better add message as a parameter. You can also use ng-submit on the form tag instead.

asgoth
  • 35,552
  • 12
  • 89
  • 98
  • looking good! Is it possible to send feedback of the "ng-submit" to an other rout/partial? My form template is "dealer-message.html" but I need the feedback on "dealer-details.html" - is this possible? – Marek123 Jan 09 '13 at 10:11
  • You need to look at [$routeParams](http://docs.angularjs.org/api/ng.$routeParams). You could define a route (in $routeProvider) '/details/feedback/:feedback'. When you change your route (with $location), you just add the feedback to the url (using encodeURIComponent): var url = '/details/feedback/' + encodeURIComponent(feedback); – asgoth Jan 09 '13 at 10:25
  • Here's another possibility to play around with for controlling when to show errors... http://stackoverflow.com/a/15842283/45767 – JeremyWeir Apr 05 '13 at 19:48
  • In fact, you _should_ use ng-submit on the form element, instead of ng-click. Because ng-click won't work if the user hits Enter. – Matthias Sep 17 '13 at 20:06