Skip to content

Latest commit

 

History

History
40 lines (31 loc) · 1.36 KB

visual_cues_with_css.md

File metadata and controls

40 lines (31 loc) · 1.36 KB

Visual Cues with CSS

Angular 2 also offers a way to define special CSS styles that add or remove classes to the form fields dependent on their state and validation following the rules shown in the table below:

Class States
ng-pristine pristine == true and dirty == false
ng-dirty dirty == true and pristine == false
ng-touched touched == true
ng-untouched touched == false
ng-valid valid == true
ng-invalid valid == false

If we want to define a style for an invalid input field, we could define a CSS rule for that.

styles.css

.ng-invalid.ng-dirty {
  border-left: 5px solid red;
}

We need now to update our index.html file to reference our new stylesheet.

index.html

<html>
  <head>
    <!-- ... -->
    <link rel="stylesheet" href="styles.css">
  </head>
  <!-- ... -->
</html>

View Example

So far all of our validation logic lives in the template and we are doing very basic validation. What if we want to use some custom validation? We need to have more control of our form, and for that we need to use the FormBuilder.

<iframe class="no-pdf" style="width: 100%; height: 300px" src="http://embed.plnkr.co/EK0xuiM1eIzEfqXrvt7Z/" frameborder="0" allowfullscren="allowfullscren"></iframe>