3

I am using this below form:

<form name="signupForm" id="register-form" ng-submit="addUser(user)" method="POST">
    <div class="row">
        <input type="email" ng-model="user.email" id="reg_email" class="form-control" placeholder="Email" required pattern="[^@\s]+@[^@\s]+\.[^@\s]+" minlength="5" maxlength="40" required>
        <input type="password" ng-model="user.password" id="reg_password" class="form-control" placeholder="Password" required='true' minlength="3" maxlength="10" required>
        <input type="submit" id="reg_submit" class="submit-input grad-btn ln-tr" value="Submit">
    </div>
</form>

When there error in form, I'm setting setCustomValidity() on input.

auth.signup($scope.userData, successAuth, function (error) {
    $rootScope.error = error.error;
    console.log("error: " , $rootScope.error);

    if($rootScope.error == "user already exists"){
        var input = document.getElementById("reg_email");
        input.setCustomValidity("$rootScope.error");
    } else{
        var input = document.getElementById("reg_email");
        input.setCustomValidity("");
        angular.element('#register-modal').modal('hide');
        document.getElementById("register-form").reset();
    }
});

Well the modal form doesn't close because of setCustomValidity() but I can't the error message I set on the input field.

Update:

Actually it is showing the error message on second click. why?

Error messages activates on second click and even when form is valid it is showing the error message.

1 Answer 1

1

I fixed it this way. Hope it helps some one.

auth.signup($scope.userData, successAuth, function (error) {
    $rootScope.error = error.error;
    if ($rootScope.error == "user already exists") {
        $scope.signupForm.reg_email.$setValidity("User already exists", false);
    }
});

Html:

<form novalidate>
    <input type="email" ng-model="user.email" id="reg_email" name="reg_email" required>
    <span ng-show="signupForm.reg_email.$invalid" class="custom-help-block">User already exists</span>
</form>

css:

.custom-help-block{
    color: red;
    margin-left: 5px;
    font-weight:bold;
}
Sign up to request clarification or add additional context in comments.

1 Comment

can you add some description of what you did there?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.