question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Proposal. Forms validations directive

See original GitHub issue

In order to add validation error state we has-danger, form-control-danger and form-control-feedback css classes on different html elements

<div class="form-group has-danger">
   <label class="form-control-label" for="title">Title</label>
   <input type="text" class="form-control form-control-danger" formControlName="title" id="title">
   <div class="form-control-feedback">Title is required</div>
</div>

With angular it looks something like this

<div class="form-group" [ngClass]="{'has-danger': adForm.controls.title.invalid && adForm.controls.title.touched}">
   <label class="form-control-label" for="title">Title</label>
   <input type="text" class="form-control" [ngClass]="{'form-control-danger': adForm.controls.title.invalid && adForm.controls.title.touched}" formControlName="title" id="title">
   <div *ngIf="adForm.controls.title.invalid && adForm.controls.title.touched" class="form-control-feedback">Title is required</div>
</div>

I’m thinking about writing few directives to automatically handle all these css classes states so it can look as simple as this:

<div ngbFormGroup>
   <label for="title">Title</label>
   <input type="text" ngbFormControl formControlName="title" id="title">
   <div ngbFormControlFeedback>Title is required</div>
</div>

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:1
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

4reactions
Jucharcommented, Nov 14, 2017

Just for other people stumbling across this, I was in the same situation and had to update the approach of @jnizet for the new bootstrap version:

import {ContentChild, Directive, HostBinding} from "@angular/core";
import {NgControl} from "@angular/forms";

@Directive({
  selector: '.form-control:not([ngbNoFormControlValidation]),[ngbFormControlValidation]'
})
export class NgbFormControlValidation {

  @ContentChild(NgControl)
  ngControl: NgControl;

  @HostBinding('class.is-invalid')
  @HostBinding('class.was-validated')
  get isInvalid() {
    return this.ngControl && this.ngControl.dirty && this.ngControl.invalid;
  }
}

Note: This does not take into account the “is-valid” state where green borders are added as I don’t need this right now.

0reactions
lonix1commented, Jun 5, 2019

ng-bootstrap-form-validation library does this automatically

Read more comments on GitHub >

github_iconTop Results From Across the Web

How To Use Custom Form Validation in Angular
Learn how to create a custom validator in Angular for both template-driven ... Directives are used for validation in template-driven forms.
Read more >
Angular Forms and Validations
We will cover both basic and advanced input validations such as: how to validate emails in angular, passwords, phone numbers and unique ...
Read more >
Working with Angular 4 Forms: Nesting and Input Validation
Validating user inputs is an essential part of any robust web application. Forms in Angular applications can aggregate the state of all inputs...
Read more >
Angular Form Validation Example Tutorial
Improve overall data quality by validating user input for accuracy and completeness. The source code for this template is on GitHub, please feel...
Read more >
The best way to implement custom validators - Angular ...
To make our custom validator accessible for template-driven forms, we need to implement our validator as a directive and provide it as NG_VALIDATORS...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found