Join egghead, unlock knowledge.

Want more egghead? It's 45% off for a limited time only!

This lesson is for members. Join us? Get access to all 3,000+ tutorials + a community with expert developers around the world.

Unlock All Content for 45% Off

Already subscribed? Sign In

Save 45% for a limited time.

Get access to all courses and lessons on egghead today.

Autoplay

    Style Validation in Angular 2 Forms

    John LindquistJohn Lindquist

    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.

    angularAngular
    2 - 6
    Code

    Code

    Become a Member to view code

    You must be a Member to view code

    Access all courses and lessons, track your progress, gain confidence and expertise.

    Become a Member
    and unlock code for this lesson
    Transcript

    Transcript

    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.

    Discuss

    Discuss