kittu
kittu

Reputation: 7008

html5 custom validation not working with angular

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.

Upvotes: 3

Views: 1462

Answers (1)

kittu
kittu

Reputation: 7008

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;
}

Upvotes: 1

Related Questions