noclist
noclist

Reputation: 1819

Using $pristine on form validation AngularJS

I have a form with some angular validation, which I don't want to run at page load. Here is a stripped down example of my form:

<form ng-submit="vm.submit()" name="form" novalidate>
    <input class="form-control" ng-model="vm.userName" required />
    <button ng-disabled="form.$invalid && !form.vm.userName.$pristine" type="submit">Log In</button>
</form>

I'm attempting to turn off initial validation with !form.vm.userName.$pristine, as the user won't have touched the username text box yet. However, this isn't working and the form validates as usual on page load. Am I missing something?

Upvotes: 1

Views: 680

Answers (1)

dfsq
dfsq

Reputation: 193261

You need to give input a name, so that ngFormController can register this element under and its validation rules. Then you would be able to check form.userName.$pristine:

<form ng-submit="vm.submit()" name="form" novalidate>
    <input class="form-control" name="userName" ng-model="vm.userName" required />
    <button ng-disabled="form.$invalid && !form.userName.$pristine" type="submit">Log In</button>
</form>

Upvotes: 2

Related Questions