Create custom validators for dynamic forms angular 2

kero

I just red angular 2 cookbook in how you I can create dynamic forms but I wander how I can add custom validators to particular field.

questions.forEach(question => {
  group[question.key] = question.required ? new FormControl(question.value || '', Validators.required)
                                          : new FormControl(question.value || '');
});

Here they form a from group to hold form inputs so what about if I want to apply a particular validation to a particular question Ex: if I has input for confirming password matching. I know that there is validateequal attribute to do this task how I can apply this validateequal or even create my own custom validation

Note that it is dynamic form which means could hold any input for example i am planning to use the same form to generate log in form which means that it only has password input , i need away to check if there is input will hold password and if any will hold password confirmation and if so then i need to check if they are matching before submit

Caleb Swank

alrighty, I think I have it.

You're going to use custom directives in order to get the Job done.

Here is a very rudimentary tutorial on custom directives https://angular.io/docs/ts/latest/guide/attribute-directives.html

if you create that however you're templating your app, you can easily modify it to fit your parameters.

in mine, i've used that example in the following way.

Your NgModule || app.module.ts

import { customDirective } from '../directive/path';

Your customDirective.ts || js

import {Directive, HostListener, Input, ElementRef} from '@angular/core';
@Directive({
   selector: '[fooSelector]'
})

export class CustomDirective {
  constructor(private el: ElementRef, private renderer: Renderer) {}
  @Input('fooSelector') testing: Object;

  //this controls event type with change being the event
  @HostListener('change') onChange(){
    this.logicFunct(this.htmlAttr); //will define htmlAttr in template
  }

  //this is the validator logic
  private logicFunct($obj: Object){
    // submit logic here - for ex:
    if($obj != 'test) {
      this.varBar = true; //check template ngIf
    }
  }
}

Your Template

<input
  type="text"
  #htmlAttr
  [fooSelector]="this.htmlAttr._value"
  *ngIf="!this.varBar">
</input>

I'm almost positive there are other better ways to do this, and if someone has one, please let us know!

Hope this helps anyone stumbling upon it.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Angular 2 form validation with custom validators ng-valid class

From Dev

Angular 2 form validation with custom validators ng-valid class

From Dev

Angular 2 Multiple validators

From Dev

How to add validators to a formModel in angular reactive forms

From Dev

Angular: How to conditionally apply Validators for Reactive forms

From Dev

Angular2 template driven forms: how to create field validation for a custom form control?

From Dev

Custom Angular Validators- Passing an Argument

From Dev

Angular 4 - Using Asynchronous custom validators

From Dev

How can I create Angular 2 custom attribute directive with dynamic name?

From Dev

Angular2 create model nested forms

From Dev

Create React Dynamic Forms

From Dev

Showing different error messages for different validators for reactive angular forms

From Dev

Adding multiple validators to FormGroup in angular2

From Dev

Angular 2 conditional Validators.required?

From Dev

Conditional Form Validators Angular2

From Dev

Angular2 - Validators.compose

From Dev

Adding multiple validators to FormGroup in angular2

From Dev

angular 2: validate a input field with built in validators

From Dev

Creating dynamic forms in angular2 using metadata

From Dev

Angular 2, dynamic forms; updateValue() do not update checkbox form control

From Dev

how to reactive validation messages in dynamic forms in angular 2

From Dev

Create dynamic custom selector

From Dev

Testing custom validators with Minitest

From Dev

CausesValidation not working with custom validators

From Dev

Spring MVC custom validators

From Dev

Django Custom Validators Not Working

From Dev

Proptypes custom validators with Flow

From Dev

create dynamic anchorName/Components with ComponentResolver and ngFor in Angular2

From Dev

Angular2: how to apply two or more validators to the same control?

Related Related

  1. 1

    Angular 2 form validation with custom validators ng-valid class

  2. 2

    Angular 2 form validation with custom validators ng-valid class

  3. 3

    Angular 2 Multiple validators

  4. 4

    How to add validators to a formModel in angular reactive forms

  5. 5

    Angular: How to conditionally apply Validators for Reactive forms

  6. 6

    Angular2 template driven forms: how to create field validation for a custom form control?

  7. 7

    Custom Angular Validators- Passing an Argument

  8. 8

    Angular 4 - Using Asynchronous custom validators

  9. 9

    How can I create Angular 2 custom attribute directive with dynamic name?

  10. 10

    Angular2 create model nested forms

  11. 11

    Create React Dynamic Forms

  12. 12

    Showing different error messages for different validators for reactive angular forms

  13. 13

    Adding multiple validators to FormGroup in angular2

  14. 14

    Angular 2 conditional Validators.required?

  15. 15

    Conditional Form Validators Angular2

  16. 16

    Angular2 - Validators.compose

  17. 17

    Adding multiple validators to FormGroup in angular2

  18. 18

    angular 2: validate a input field with built in validators

  19. 19

    Creating dynamic forms in angular2 using metadata

  20. 20

    Angular 2, dynamic forms; updateValue() do not update checkbox form control

  21. 21

    how to reactive validation messages in dynamic forms in angular 2

  22. 22

    Create dynamic custom selector

  23. 23

    Testing custom validators with Minitest

  24. 24

    CausesValidation not working with custom validators

  25. 25

    Spring MVC custom validators

  26. 26

    Django Custom Validators Not Working

  27. 27

    Proptypes custom validators with Flow

  28. 28

    create dynamic anchorName/Components with ComponentResolver and ngFor in Angular2

  29. 29

    Angular2: how to apply two or more validators to the same control?

HotTag

Archive