0

I have created a custom directive for my email like @Daniel Francisco Sabugal in this thread:

Angular2 - Control Validation on blur

and it works great, however, I need just a small tweek. My submit button should be submitted only if form is valid, so

[disabled]="!form.valid"

is working fine, but... It stays disabled all the way until i click outside the field, even if the email is in a valid state. Can I somehow put in that condition, to enable button while typing IF the email pattern is correct?

The code from the thread above:

@Directive({
    selector: '[validate-onblur]',
    host: {
        '(focus)': 'onFocus($event)',
        '(blur)' : 'onBlur($event)'
    }
})

export class ValidateOnBlurDirective {

private validators: any;
private asyncValidators: any;
private hasFocus = false;

constructor(public formControl: NgControl) {
}

onFocus($event) {
    this.hasFocus = true;
    this.validators = this.formControl.control.validator;
    this.asyncValidators = this.formControl.control.asyncValidator;
    this.formControl.control.clearAsyncValidators();
    this.formControl.control.clearValidators();
    this.formControl.control.valueChanges
        .filter(() => this.hasFocus)
        .subscribe(() => this.formControl.control.markAsPending());
}

onBlur($event) {
    this.hasFocus = false;
    this.formControl.control.setAsyncValidators(this.asyncValidators);
    this.formControl.control.setValidators(this.validators);
    this.formControl.control.updateValueAndValidity();
}
}

EDIT 1

@JBNizet solved it with this example:

http://plnkr.co/edit/O39KKQIYeKdKt8RCAzHw?p=preview

Community
  • 1
  • 1
Nemanja G
  • 1,760
  • 6
  • 29
  • 48
  • Why don't you just use the standard email validator? https://github.com/angular/angular/blob/4.0.0/packages/forms/src/validators.ts#L82. If you only want to *display* the validation error on blur, then do that, rather than *applying* the validation on blur. – JB Nizet May 08 '17 at 12:26
  • @JBNizet I need my validation messages to dissapear when I start typing again, so when they display on blur, i want to hide them when I start typing again – Nemanja G May 08 '17 at 12:31
  • Then do that. Simple example: http://plnkr.co/edit/O39KKQIYeKdKt8RCAzHw?p=preview – JB Nizet May 08 '17 at 12:35
  • @JBNizet plnkr example with added event `(ngModelChange)` which changes as soon as you change in input field http://plnkr.co/edit/YfgT4nRXaSl5mYMdhn7K?p=preview – mayur May 08 '17 at 12:59
  • @JBNizet didn't know I can do it like this, setting focus value, helped a ton. – Nemanja G May 08 '17 at 13:02
  • @JBNizet just a follow up question, why does (focus)="focus = true" (blur)="focus = false", and && !focus remove all validation messages? I would like my focus on email to remove validation message ONLY for the email, same for password – Nemanja G May 09 '17 at 15:14
  • Probably because there is a bug in your code. http://plnkr.co/edit/Dkm64PowR06yvEOQvxqD?p=preview – JB Nizet May 09 '17 at 16:04
  • It works perfectly when I have only one input inside the form, but when I got 2 input fields it still works but when I focus one field, it removes message from every field inside the form. And I have to write 'focus == true', can't use 'focused == true'. Maybe I made some mistake , thanks for the help tho @JBNizet – Nemanja G May 10 '17 at 09:26

0 Answers0