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

Validators.email should allow null/empty values #16183

Closed
klinem opened this issue Apr 20, 2017 · 19 comments
Closed

Validators.email should allow null/empty values #16183

klinem opened this issue Apr 20, 2017 · 19 comments

Comments

@klinem
Copy link

klinem commented Apr 20, 2017

I'm submitting a ... (check one with "x")

[ ] bug report => search github for a similar issue or PR before submitting
[x] feature request
[ ] support request => Please do not submit support request here, instead see https://github.com/angular/angular/blob/master/CONTRIBUTING.md#question

Current behavior
Controls using the email validator are invalid if no value is present.

Expected behavior
The email validator should allow null/empty values.

Minimal reproduction of the problem with instructions
http://plnkr.co/edit/tsQ3C5tg3ba9xYAWHdLO?p=preview

What is the motivation / use case for changing the behavior?

Please tell us about your environment:

  • Angular version: 4.x.x

  • Browser: all

  • Language: all

@dawidgarus
Copy link

+1
It's worth mentioning, that current behavior is inconsistent with other validators.
For example, minlength allows empty strings.

Validators.minLength(3)({ value: '' }) === null
Validators.minLength(3)({ value: 'a' }) !== null
Validators.minLength(3)({ value: 'ab' }) !== null
Validators.minLength(3)({ value: 'abc' }) === null

@crain
Copy link

crain commented May 31, 2017

There is a pull request for this #16902

@rehbein-dieter
Copy link

A workaround in template driven forms (set [email] to false, when value is empty. Ensure to initialize the model value with an empy string):

<input [(ngModel)]="model.email" [email]="model.email!==''">

@jnizet
Copy link
Contributor

jnizet commented Aug 2, 2017

Please fix this for 5.0, since it's a breaking change, otherwise we're going to have to wait for 6 more months and it will be even more painful.

@danwulff
Copy link

@rehbein-dieter Nice simple workaround.

@richmondp
Copy link

@rehbein-dieter Thanks for the simple workaround. Do you get a console error when you use it though? I get the following error when I leave the field (empty) then go back and add a character.

ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked. Previous value: 'null'. Current value: 'true'

@sijovijayan
Copy link

@rehbein-dieter Nice workaround. Thank you very much !!!

@jloosli
Copy link

jloosli commented Sep 21, 2017

@rehbein-dieter or anyone else, is there a similar workaround for when you're using reactive forms e.g.:

<input formControlName="email" >

@lazarljubenovic
Copy link
Contributor

@jloosli A workaround for both types of forms is to simply wrap is in your own custom validator, check the special case and feed the control to Validator.email.

@jloosli
Copy link

jloosli commented Sep 29, 2017

For what it's worth, as a workaround, I add and remove the email validator based on whether or not the value is empty:

const emailField = this.appointment.get('email');
emailField.valueChanges
  .forEach(email => {
    if (!email) {
      emailField.setValidators([]);
    } else {
      emailField.setValidators([Validators.email]);
    }
  });
}

@davidshepherd7
Copy link

For reactive forms this validator works for me:

function emailOrEmpty(control: AbstractControl): ValidationErrors | null {
    return control.value === '' ? null : Validators.email(control);
}

@CDDelta
Copy link

CDDelta commented Jan 18, 2018

@Toxicable @kara can we get this fixed before v6 is released since it is a breaking change?

@IgorMinar IgorMinar added the feature Issue that requests a new feature label Jan 24, 2018
@ngbot ngbot bot added this to the Backlog milestone Jan 24, 2018
@IgorMinar IgorMinar added type: bug/fix feature Issue that requests a new feature and removed feature Issue that requests a new feature labels Jan 24, 2018
@RealZimboGuy
Copy link

for anyone else wanting a workaround for this, declare a local variable
validateEmail = "[a-zA-Z0-9._-]+[@]+[a-zA-Z0-9-]+[.]+[a-zA-Z]{2,6}";

then in your form group dont use email and use a pattern
formGroup: ['', Validators.pattern(this.validateEmail)]

@trotyl
Copy link
Contributor

trotyl commented Feb 20, 2018

Fixed by #20869, which has landed in 6.0.0-beta.4, can be closed now.

@pawer13
Copy link

pawer13 commented Feb 27, 2018

Working with version 5.X , I'm using the following validator:

(c: AbstractControl) => { if (!c.value) return null; return Validators.email(c);}

@pkozlowski-opensource
Copy link
Member

Fixed in 140e7c0

@prasad-desilva
Copy link

prasad-desilva commented Apr 18, 2018

Just a workaround
<input [(ngModel)]="model.email" #email="ngModel" [email]="model.email != null && model.email !==''">

@ikomangmahendra
Copy link

@davidshepherd7 Thanks Bro

@angular-automatic-lock-bot
Copy link

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Sep 14, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests