Angular JS Validation – How to turn off required for a single field

Issue

I’m trying to turn off the required attribute for a single input field in an Angular form on submit, if a certain condition is not met. I have tried the following:

form.ChangeCreditCard.$setValidity("required", false);

And even manually setting the $valid state for the field:

form.ChangeCreditCard.$valid = true;

But the field and thus the form is still invalid. Is there any way I could turn off validation on a single field in this way?

Solution

ngRequired https://docs.angularjs.org/api/ng/directive/ngRequired

The directive sets the required attribute on the element if the
Angular expression inside ngRequired evaluates to true. A special
directive for setting required is necessary because we cannot use
interpolation inside required. See the interpolation guide for more
info.

Answered By – krutkowski86

Answer Checked By – Mildred Charles (AngularFixing Admin)

Leave a Reply

Your email address will not be published.