0

I am new in angular 2, i am making a "User Register" form but it shows error in "Property does not exist on type" in Phone number Validation.

I am compiling in JIT and AOT.

In JIT compiler shows error message and run my user register form it is working properly. but when i compile with AOT shows compilation error.

I am reading the angular 2 form validation official doc here is the link Angular 2 form validation

Below is my Html Form and compiler Output

Html Form

<form class="ui large form" method="post" (ngSubmit)="onSubmit(registerForm.form.valid)" #registerForm="ngForm" novalidate>
        <sm-loader [complete]="!formSubmited" class="inverted" text="Loading..."></sm-loader>
        <div class="form-group">
            <div class="col-md-12 img-bottom">
                <div class="col-md-offset-4">
                    <img (click)="profileImage.click()" class="img img-responsive img-circle ui middle aligned tiny circular image profileImage"
                        [src]="profileImageSrc" />
                    <input class="form-control user-reg-img" (change)="fileChangeEvent($event)" type="file" accept="image/*" #profileImage [hidden]="true"
                    />
                </div>
                <div class="col-md-12 image-padding">
                    <span class="user-reg-img-name" (click)="profileImage.click()">{{profileImageName}}</span>
                </div>
            </div>
        </div>
        <div class="form-group">
            <label><strong>Signup with Email Address:</strong></label>
        </div>

        <div class="form-group">
            <p class="success text-center">{{successMessage}}</p>
            <p class="error text-center">{{errorMessage}}</p>
            <div class="field">
                <input type="text" name="name" placeholder="Name" [(ngModel)]="register.name" #name="ngModel" required class="form-control input-reg"
                />
                <div [hidden]="name.valid || name.pristine" class="error text-left">
                    Name is required
                </div>
            </div>
        </div>

        <div class="form-group">
            <div class="field">
                <input class="form-control input-reg" type="email" name="email" placeholder="Email" [(ngModel)]="register.email" #email="ngModel"
                    required />
                <div [hidden]="email.valid || email.pristine" class="error text-left">
                    Email is required
                </div>
            </div>
        </div>


        <div class="form-group">
            <input type="text" id="mobile" class="form-control  input-reg" required minlength="10" maxlength="10" name="mobile" [(ngModel)]="register.mobile"
                placeholder="Phone Number" #mobile="ngModel" pattern="[0-9]+">
            <div *ngIf="mobile.errors && (mobile.dirty || mobile.touched)">
                <div class="error text-left" [hidden]="!mobile.errors.required">
                    Mobile Number is required
                </div>
                <div class="error text-left" [hidden]="!mobile.errors.minlength">
                    Mobile Number must be at least 10 characters long.
                </div>
                <div class="error text-left" [hidden]="!mobile.errors.maxlength">
                    Monile Number cannot be more than 10 characters long.
                </div>
            </div>
        </div>

        <div class="form-group">
            <div class="field">
                <input class="form-control input-reg" type="password" name="password" placeholder="Password" [(ngModel)]="register.password"
                    #password="ngModel" required />
                <div [hidden]="password.valid || password.pristine" class="error text-left">
                    Password is required
                </div>
            </div>
        </div>

        <div class="form-group">
            <input type="checkbox" name="isAgree" [(ngModel)]="register.isAgree" #isAgree="ngModel" required />
            <label id="label-terms-cond">I Agree to Post Bucks <a href="#">Terms & Conditions</a></label>
            <div [hidden]="!showTermsAgreeErrorMsg" class="error text-left">
                Please agree the terms and conditions to continue
            </div>
        </div>

        <div class="form-group">
            <textarea rows="4" class="form-control font-small terms-cond-textarea" rows="7">You must follow any policies made available to you within the Services.</textarea>
        </div>

        <div class="form-group">
            <button class="fluid yellow large ui button btn btn-block btn-warning reg-btn-submit" type="submit">Signup</button>
        </div>
    </form>

AOT Output

AOT error

Thanks in advance

Vikram

Community
  • 1
  • 1
Sharma Vikram
  • 2,440
  • 6
  • 23
  • 46

2 Answers2

1

It looks the JIT compiler will never check the type of mobile.errors, but AOT check it at compilation by looking this line :

<div *ngIf="mobile.errors && (mobile.dirty || mobile.touched)">

According that in this line mobile.errors is a boolean, you'll obtain your traces... I can't reproduce this bug (version problem ?), but maybe the following code will fix it :

<div *ngIf="(mobile.errors != null) && (mobile.dirty || mobile.touched)">

There is an opened issue here : https://github.com/angular/angular/issues/11425

Karbos 538
  • 2,977
  • 1
  • 23
  • 34
-1

Here is my answer, there is actual problem of required which is not found under mobile.errors object. below is my single line code.

!mobile.errors['required']

If object field not found then we use as array in template.

Sharma Vikram
  • 2,440
  • 6
  • 23
  • 46
  • By this line of code your are using raw Javascript map instead of the typescript object (that is a boolean). It's a workaround. – Karbos 538 Dec 13 '16 at 08:51