Angular validate password field with FormGroup

0

I have the following code:

TS Code:

...
onRegisterForm: FormGroup;
ngOnInit() {

    this.onRegisterForm = this._fb.group({
      nome: ['', Validators.compose([
        Validators.required
      ])],      
      email: ['', Validators.compose([
        Validators.required
      ])],
      senha: ['', Validators.compose([
        Validators.required,
        Validators.minLength(6)
      ])],
      confirm_senha: ['', Validators.compose([
        Validators.required
      ])]
    });
  }
...

HMTL Code:

<ion-item>
        <ion-label floating>Senha</ion-label>
        <ion-input type="{{isPassword ? 'password' : 'text' }}" [(ngModel)]="userData.senha" formControlName="senha" minlength="6"></ion-input>
        <button ion-button clear color="dark" type="button" class="btn-icon-eye" item-right (click)="showPassword()">
          <ion-icon name="ios-eye-outline" class="icon-eye" item-right></ion-icon>
        </button>
      </ion-item>
      <p ion-text color="danger" class="has-error" *ngIf="onRegisterForm.get('senha').touched && onRegisterForm.get('senha').hasError('required')">Este campo é obrigatório.</p>
      <p ion-text color="danger" class="has-error" *ngIf="onRegisterForm.get('senha').hasError('minLength')">A senha deve conter no mínimo 6 caracteres.</p>

The two validators work perfectly (the problem is in the display of the messages).

When I click on the field and leave without filling in the field, the message: "This field is required." . However, when I type less than six characters the message: "The password must contain at least 6 characters." does not appear!

Analyzing the code can not find the error!

    
asked by anonymous 14.03.2018 / 16:37

1 answer

1

Some time ago I discovered the problem and forgot to post the answer.

The error was in:

onRegisterForm.get('senha').hasError('minLength')

Where minLength should be in small minlength

In this way:

onRegisterForm.get('senha').hasError('minlength')
    
15.05.2018 / 15:59