How to develop a directive that modifies element styles based on form validation status

0 votes
Can Someone help me regarding How to develop a directive that modifies element styles based on form validation status?
2 days ago in Angular by Nidhi
• 14,600 points
20 views

1 answer to this question.

0 votes

Here's a concise directive that automatically applies CSS classes based on Angular form control validation status:

import { Directive, ElementRef, Input, Renderer2 } from '@angular/core';

import { NgControl } from '@angular/forms';

@Directive({

  selector: '[appValidationStyle]'

})

export class ValidationStyleDirective {

  @Input() appValidationStyle: NgControl;

  constructor(private el: ElementRef, private renderer: Renderer2) {}

  ngOnInit() {

    this.appValidationStyle.statusChanges?.subscribe(() => {

      this.updateStyles();

    });

  }

  private updateStyles() {

    const control = this.appValidationStyle;

        this.renderer.removeClass(this.el.nativeElement, 'is-valid');

    this.renderer.removeClass(this.el.nativeElement, 'is-invalid');

        if (control.touched || control.dirty) {

      if (control.valid) {

        this.renderer.addClass(this.el.nativeElement, 'is-valid');

      } else {

        this.renderer.addClass(this.el.nativeElement, 'is-invalid');

      }

    }

  }

}

Usage:

<input 

  formControlName="email"

  appValidationStyle

  [appValidationStyle]="form.get('email')">

answered 2 days ago by anonymous

Related Questions In Angular

0 votes
1 answer

How to develop a pipe that formats phone numbers based on locale?

You can use the libphonenumber-js library for ...READ MORE

answered 2 days ago in Angular by anonymous
27 views
0 votes
1 answer

How to implement a directive that manages focus on dynamically added elements?

To implement a directive that manages focus ...READ MORE

answered 2 days ago in Angular by anonymous
32 views
0 votes
1 answer

How to build a directive that listens for specific keyboard shortcuts?

You can use the @HostListener decorator to ...READ MORE

answered 2 days ago in Angular by anonymous
42 views
0 votes
0 answers

How do Observables improve API call handling in Angular?

With the help of an example, can ...READ MORE

Mar 3 in Angular by Nidhi
• 14,600 points
107 views
0 votes
0 answers

What’s the difference between Observables and Promises?

With the help of an example, can ...READ MORE

Mar 3 in Angular by Nidhi
• 14,600 points
76 views
0 votes
0 answers

What type of operation do RxJS operators allow for observables?

With the help of an example, can ...READ MORE

Mar 3 in Angular by Nidhi
• 14,600 points
85 views
0 votes
0 answers

When to use switchMap vs concatMap?

With the help of an example, can ...READ MORE

Mar 3 in Angular by Nidhi
• 14,600 points
111 views
0 votes
1 answer

How to implement a directive that adds tooltips to form controls dynamically?

Custom Tooltip Directive 1. Create the Directive import { ...READ MORE

answered 2 days ago in Angular by anonymous
19 views
0 votes
1 answer

How to create a custom directive that integrates with external charting libraries?

Compact Directive for External Chart Integration import { ...READ MORE

answered 2 days ago in Angular by anonymous
22 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP