⚠️ This lesson is retired and might contain outdated information.

Style Validation in Angular 2 Forms

John Lindquist
InstructorJohn Lindquist
Share this video with your friends

Social Share Links

Send Tweet
Published 8 years ago
Updated 2 months ago

Inputs using Angular 2’s ngModel automatically apply style classes of .ng-valid and .ng-invalid each time the input’s validity changes. These classes allow you easily add your own styles simply by declaring the styles in your Component decorator.

[00:00] If you use the dev tools to select one of the inputs inside of your form, you'll notice these classes NG untouched, NG pristine, and NG valid. Let's focus on NG valid, because if I change this input to be invalid, this field is required, you'll see that this now has NG invalid instead of NG valid.

[00:21] What that allows us to do is to style these fields with our own custom styles based on those classes. If I add a style of NG invalid and I say it's a border, it's a three pixel solid red, and I hit save. You'll see now we're green. If I delete this, this field becomes red.

[00:44] If I go ahead and duplicate that so we have an NG valid class, we'll say this one is green and I hit save, you'll see it starts as green. In fact, they're both green. If I delete this, this one turns red and this one's green.

[00:59] Each input inside of your form is going to provide these convenient validity classes you can use, to simply create your own CSS classes and apply a style based on whether it's valid, or based on whether it's invalid.

egghead
egghead
~ an hour ago

Member comments are a way for members to communicate, interact, and ask questions about a lesson.

The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io

Be on-Topic

Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.

Avoid meta-discussion

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

Code Problems?

Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context

Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

Markdown supported.
Become a member to join the discussionEnroll Today