Proposal. Forms validations directive
See original GitHub issueIn 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:
- Created 6 years ago
- Reactions:1
- Comments:5 (1 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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:
Note: This does not take into account the “is-valid” state where green borders are added as I don’t need this right now.
ng-bootstrap-form-validation library does this automatically