Friday, 23 August 2013

AngularJS make custom form field.error act like "required"

AngularJS make custom form field.error act like "required"

I am validating my forms both in the client and in my API when sent. If
validation fails in my backend I want to show identical messages as I do
when my client validation fails. To do this I use the following piece of
code when a field fails in the backend.
#Template
<span class="registration-error"
ng-show="accPasswordForm.password.$error.required">*</span>
<span class="registration-error"
ng-show="accPasswordForm.password.$error.authfail">Authentication
failed</span>
<input ng-model="changePasswordForm.password" name="password"
type="password" authfail required/>
#Controller ( I set this if the password field throws a validation error
in my API )
scope.myForm.password.$setValidity('authfail', false);
This works fine, but the problem is that I want this $setValidity to be
set to true when I start typing in the password field again. Same behavior
as the required error has. Ive tried in different ways to watch the field
input but I seem to always fail getting it to work.
Someone able to give me a hint/code snippet on how I can manage to get
this working?

No comments:

Post a Comment