I am creating an Angular registration form with three inputs: email , password and passwordConfirm . My controller looks like this:
app.controller('SignupController', function ($scope, User) { $scope.user = {}; $scope.signup = function() { User.save($scope.user, function(success) {
What it is - sending a request to my API when a user submits a registration form. If response 422 returned (which means validation errors), I scroll through them and set the corresponding inputs to invalid data depending on the returned API.
Let's look at an example of an already registered email address. I am showing a validation error in my view as follows:
<small class="error" ng-show="signupForm.email.$error.exists">Email address taken, please use another</small>
All this works fine, but my user is now stuck, because when they try to change the email address to another, the validity of the email field does not change to allow them to resubmit the form (I disabled the submit button based on the validity of the form).
Basically, I need the validation property (in this case exists ) to be reset to true after the user changes the input model. What is the best way for me to do this?
Edit: A little brain wave hit me after posting, I added this to my controller:
$scope.$watch('user.email', function() { // Reset exists validation property back to true $scope.signupForm.email.$setValidity('exists', true); });
which seems to work. When the user changes the value of the model after the input has been set as invalid, he will return it to action. But this does not seem to me the best way to do this. Does anyone know better?