Angular: Custom validation with parameter

Discussão em 'Angular' iniciado por Gelso77, Dezembro 3, 2024.

  1. Gelso77

    Gelso77 Guest

    I am trying to implement a verify password validation with Angular 6 using Reactive Form but I can't get the best way to do it, below an example that works with "1234" but I would like to pass the value of the password control instead. I tried with ValidatePWD(this) but doesn't work either.

    //component.ts
    import { ValidatePWD } from './compare.validator';

    this.form = this._fb.group({
    'user': ['', Validators.compose([Validators.required])],
    'password': ['', Validators.compose([Validators.required])],
    'verifypassword': ['', [Validators.required, ValidatePWD]],

    });


    //compare.validator.ts
    import { AbstractControl, FormGroup } from '@angular/forms';
    export function ValidatePWD(control: AbstractControl ) {
    if (control.value != "1234") {
    return { validPWD: true };
    }
    return null;
    }


    <div class="form-group">
    <label>Password: {{model.password}}</label>
    <input [(ngModel)]="model.password" [formControl]="password" class="form-control" type="password">
    </div>

    <div class="form-group">
    <label >Verify Password</label>
    <input[(ngModel)]="model.verifypassword" [formControl]="verifypassword" class="form-control" type="password">
    </div>


    [​IMG]

    Continue reading...

Compartilhe esta Página