How to set Twitter Bootstrap class=error based on AngularJS input class=ng-invalid?
Solution 1
You can easily do it with ng-class
directive:
<form name="myForm">
<div class="control-group" ng-class="{ error: myForm.salt.$invalid }">
<label class="control-label" for="salt">Salt: </label>
<div class="controls">
<input type="text" name="salt" ng-model="salt" ng-pattern="hexPattern">
</div>
</div>
</form>
http://plnkr.co/edit/RihsxA?p=preview
EDIT
Example with bootstrap 3.3.5 and angular 1.4.2:
<form name="myForm">
<div class="form-group" ng-class="{ 'has-error': myForm.salt.$invalid }">
<label class="control-label" for="salt">Salt: </label>
<input class="form-control" type="text" name="salt" ng-model="salt" ng-pattern="hexPattern">
</div>
</form>
http://plnkr.co/edit/5JNCrY8yQmcnA9ysC7Vc?p=preview
Solution 2
I think a better solution is to just add your own CSS rules. It makes the code much simpler and, as a bonus, you can set the rule to only apply to "dirty" elements. That way fields will only be highlighted after the user has tried to enter something.
In my application I've just added this css, based on an example in the AngularJS forms documentation.
/* Forms */
.ng-invalid.ng-dirty {
border-color: red;
outline-color: red;
}
.ng-valid.ng-dirty {
border-color: green;
outline-color: green;
}
Sunil486
Kevin Hakanson (@hakanson) is an experienced Software Architect focused on highly scalable web applications, especially the JavaScript and security aspects. His background includes both .NET and Java, but he is most nostalgic about Lotus Notes. He has been developing professionally since 1994 and holds a Master’s degree in Software Engineering. When not staring at a computer screen, he is probably staring at another screen, either watching TV or playing video games with his family.
Updated on July 05, 2022Comments
-
Sunil486 almost 2 years
I have a problem where my CSS is not taking effect (in Chrome), and I think there is some conflict with Twitter Bootstrap.
input.ng-invalid { border-color: red; outline-color: red; }
My pattern is defined in my controller as:
$scope.hexPattern = /^[0-9A-Fa-f]+$/;
And copying the HTML from the live DOM, I see that both
ng-invalid
andng-invalid-pattern
are set, so myng-pattern
must be working.<div class="control-group"> <label class="control-label" for="salt">Salt: </label> <div class="controls"> <input type="text" id="salt" ng-model="salt" ng-pattern="hexPattern" class="ng-dirty ng-invalid ng-invalid-pattern"> </div> </div>
I see that in the "Validation states" section of the Forms section of Twitter Bootstrap Base CSS, I see I need to add the
error
class to the control-group div.<div class="control-group error">
Question: How to set the
class=error
based on the child inputclass=ng-invalid
? Can this be done with some soft of ng-class expression? Can I set this via code in the controller? Is there a way to ".$watch" the pattern evaluation like property changes? Any other ideas to get my "red" outline? -
Sunil486 about 11 yearsThis worked for me once I added
name="salt"
instead of justid="salt"
-
J86 over 10 yearsFor me too, I had to add the
name=""
attribute for this to work. -
Drellgor over 10 yearsThis didn't work for me and I'm not sure why. Has this changed some how with the newest version of AngularJS?
-
Drellgor over 10 yearsFigured it out. Bootstrap's class name is "has-error". The dash caused a javascript error so I had to wrap it in single quotes.
-
Jesse Webb about 10 yearsNote: In the attribute
ng-class="{ error: myForm.salt.$invalid }"
, thesalt
term is a reference to theinput
withname="salt"
, not theng-model
. -
Anton Hughes almost 10 yearsThis was great, but like what @Drellgor said, you need the has error. like so ng-class="{'has-error': myForm.salt.$invalid}"
-
Artem almost 9 years@ConnorLeech added example for boostrap 3.3.5