Angular 2 form elements as components - validation does not work

i implemented this solution for my project http://blog.rangle.io/angular-2-ngmodel-and-custom-form-components/ basically you create a component with a form element and put it on the form, the problem is that when I want to create a validator that checks the dependency between fields

import {Directive, Attribute} from '@angular/core';

    import {
      NG_VALIDATORS,
      AbstractControl,
    } from '@angular/forms';

    @Directive({
      selector: '[validateEqual][ngModel]',
      providers: [
        {provide: NG_VALIDATORS, useExisting: ParentFieldNotNullValidator, multi: true}
      ]
    })
    export class ParentFieldNotNullValidator {
      constructor(@Attribute('validateEqual') public validateEqual: string) {
      }

      validate(c: AbstractControl): {[key: string]: any} {
        if (!c.value) {
           return null;
        }
        let e = c.root.get(this.validateEqual);

        if (e && e.value) {
          console.log('ERROR 1');
          return null;
        }
        console.log('error 2');
        return {validateError: "message"} }}

in the standard template it controls its work, but in this implementation it does not, and I get this stream a screenshot from the web browser

this validator should only write "ERROR 1" when the parent field exists and is not null

what am I doing wrong? my html:

<form #form="ngForm" (ngSubmit)="onSubmit(form.value)">
  <div class="u-space-bottom-8">
    <form-input
      cannotContainSpace
      minlength="4"
      required
      name="username"
      [(ngModel)]="user.username">
      >
    </form-input>
  </div>
  <div class="u-space-bottom-8">
    <form-input
      validateEqual="username"
      type="password"
      required
      name="password"
      [(ngModel)]="user.password">
      >
    </form-input>
  </div>
  <button
    class="c-btn c-btn--default u-h-10 u-bg-gray-16 u-paint-white-1"
    type="send"
    [disabled]="!form.valid"
  >
    {{'btn_login'|translate}}
  </button>
</form>
+4
1

, , :

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

import {
  NG_VALIDATORS,
  AbstractControl,
} from '@angular/forms';

@Directive({
  selector: '[validateEqual][ngModel]',
  providers: [
    {provide: NG_VALIDATORS, useExisting: ParentFieldNotNullValidator, multi: true}
  ]
})
export class ParentFieldNotNullValidator {
  constructor() {
  }
  @Input('validateEqual') parentValue: string;
  validate(c: AbstractControl): {[key: string]: any} {
    if (!c.value) {
      return null;
    }
    if (this.parentValue) {
      return null;
    }
    return {parentFieldNotNull: "message"}
  }
}

html

<form #form="ngForm" (ngSubmit)="onSubmit(form.value)">
  <div class="u-space-bottom-8">
    <form-input
      cannotContainSpace
      minlength="4"
      required
      name="username"
      [(ngModel)]="user.username">
      >
    </form-input>
  </div>
  <div class="u-space-bottom-8">
    <form-input
      [validateEqual]="user.username"
      type="password"
      required
      name="password"
      [(ngModel)]="user.password">
      >
    </form-input>
  </div>
  <button
    class="c-btn c-btn--default u-h-10 u-bg-gray-16 u-paint-white-1"
    type="wyslij"
    [disabled]="!form.valid"
  >
    {{'btn_login'|translate}}
  </button>
</form>
+1

Source: https://habr.com/ru/post/1674544/


All Articles