yuyang041060120 / ng2-validation

angular2 validation
https://yuyang041060120.github.io/ng2-validation/index.html
MIT License
613 stars 214 forks source link

email validation on initialized value fails #88

Closed Starojitski closed 7 years ago

Starojitski commented 7 years ago

The problem is that email validator wrongly validated initial value to invalid.

I was building reactive form for a subscriber (a model acquired from a service): in component.ts

    let email = subscriber.Email;
    ...
    Email: this.formBuilder.control(email, [Validators.required, CustomValidators.email]),

in template:

        <div class="form-group" [ngClass]="{ 'has-error': f.submitted && !emailControl.valid}">
          <input id="email" type="email" class="form-control" formControlName="Email"/>
          <div>{{emailControl.errors|json}} - {{emailControl.valid}}</div>
          <div *ngIf="f.submitted && !emailControl.valid" class="help-block">Email is required</div>
        </div>

produces image if i edit the field but leave the same email then validation changes to image

If use hardcoded email value:

    let email = 'hardcoded@email.com';
    ...
    Email: this.formBuilder.control(email, [Validators.required, CustomValidators.email]),

then i have no problem image

How do i solve this?

Starojitski commented 7 years ago

found the problem! it turns out that the email was padded with spaces which i could not see in console.log.