Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error Messages on FormGroup Validation #475

Closed
KingdaCrash opened this issue Aug 23, 2017 · 3 comments
Closed

Error Messages on FormGroup Validation #475

KingdaCrash opened this issue Aug 23, 2017 · 3 comments

Comments

@KingdaCrash
Copy link

Hi there,

I am running into problems using custom validators on a DynamicFormGroupModel. I am using the following construct:

this.passwordField = new DynamicInputModel({
      id: 'password',
      label: 'Password',
      inputType: 'password',
      validators: {
        required: null,
        minLength: this.pwLength
      }
    });
    this.passwordRetypeField = new DynamicInputModel({
      id: 'passwordRetype',
      label: 'Retype Password',
      inputType: 'password',
      validators: {
        required: null,
        minLength: this.pwLength
      }
    });
this.passwordSetterGroup = new DynamicFormGroupModel({
      id: 'passwordSetterGroup',
      legend: 'Set your password',
      group: [
        this.passwordField,
        this.passwordRetypeField,
      ],
      validator: {matchingPasswords: null, validators: null},
      errorMessages: {
        noMatchingPasswords: 'Passwords need to match',
      }
    });

My validator is the following:

import { FormControl } from '@angular/forms';
export function matchingPasswords(control: FormControl): {[s: string]: boolean} {
    const password = control.get('password');
    const confirmPassword = control.get('passwordRetype');
    if (password.value !== confirmPassword.value) {
      console.log('passwords do NOT match');
      return {
        'noMatchingPasswords': true
      };
    } else {
        console.log('passwords match');
      return null;
    }
  }

The validation is working great so far and I am not able to save the form without matching passwords. However, any validation fail on inner fields (password fields) leads to an empty errorMessage on the formGroup. The use case for this error is: I am typing in less than 8 characters in both password fields, but both fields are matching. Hence, the custom validation succeeds. This results in an empty error box displayed for the form.

What do I have to do, to get rid of this error box, or alternatively: display the inner error?

Thanks in advance and have a great day!

@udos86
Copy link
Owner

udos86 commented Aug 23, 2017

@KingdaCrash Hi, thanks for reporting this!

Please give me a couple of days to recreate that use case in my sample app.

There really could be some unexpected behavior.

@udos86
Copy link
Owner

udos86 commented Aug 31, 2017

@KingdaCrash After some research I can confirm this is a bug.

Error Messages are not shown for FormGroups at the moment.

I'm already working on a patch.

@udos86
Copy link
Owner

udos86 commented Aug 31, 2017

Bug originated in the way Angular is currently handling errors on a FormGroup :
angular/angular#10530

There'll be a patch by the end of this week!

@udos86 udos86 closed this as completed in e51473c Aug 31, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants