1

I have a form field that is required and email type. I want the form validation work when I lose focus from my field, so I am using: ng-model-options="{ updateOn: 'blur' }"

example:

<input class="sng-size-input" type="email" required
                            ng-keyup="clearValidation(emailform.email_interface_form,emailform.email_interface_form.fromAddress)"
                            ng-model="emailSelectedValue.fromAddress" name="fromAddress"
                            ng-model-options="{ updateOn: 'blur' }"
                            value="{{emailSelectedValue.fromAddress}}"
                            ng-disabled="!emailSelectedValue.enabled" id="emailFromAddress">
                            <span class="sng_form_error"
                            ng-show="emailform.email_interface_form.fromAddress.$error.email"
                            data-i18n="Not Valid e-mail"> </span> <span
                            class="sng_form_error"
                            ng-show="emailform.email_interface_form.fromAddress.$error.required"
                            data-i18n="Required"></span>

when back to the field and start typing again I want my errors messages to disappear, so I am using:

$rootScope.clearValidation = function(form,field){ field.$setPristine(true);

};

my red css on the field disappears, but the error messages are not. What am I missing?

my css:

.form-validation .ng-invalid.ng-dirty {
    border-color: #e94b3b;
}

Thanks,

1 Answer 1

2

because in that time your input has errors and your ng-show expression returning true, you can use $setValidity(validationErrorKey, isValid); to set this input back to valid for more check documentation

Sign up to request clarification or add additional context in comments.

5 Comments

I have added to clearValidation this: form.$setValidity("fromAddress",true); and it still not working
my mistake.... I changed to field.$setValidity("email",true); and this works. Thanks,
there is one issue with required state... once I change to field.$setValidity("required",true); when come back to empy field with required, then add some text and remove it again I don't get required error note. is there away to handle it?
you should .$setValidity("required",true); only in that case when your input value is not empty you can check it with $viewValue
I tried it and for some reason it still don't work. it looks like it remembers the last time I changed the validity to true.

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.