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

[Angular] Identifier 'required' is not defined. '__type' does not contain such a member #152

Closed
Kurtz1993 opened this issue Aug 28, 2017 · 22 comments

Comments

@Kurtz1993
Copy link

property errors of AbstractControlDirective

I have the following code:

<div>
    <label for="fullname">Passenger name:</label>
    <input
      type="text"
      name="fullname"
      required
      #fullname="ngModel"
      [ngModel]="detail?.fullname">

      <div class="error" *ngIf="fullname.errors?.required && fullname.dirty">
        Passenger name is required.
      </div>
  </div>

By using a property inside the errors property of the ngModel reference it shows me that error.
This shouldn't be an error since it's valid syntax for template-driven forms.

@Kurtz1993
Copy link
Author

Closing because duplicate of #149

@Ajinath07
Copy link

I have tried with above approach but it failed miserably.

Please look at this code -

This field is required
Minimum length should be {{username.errors.minlength.requiredLength}}

Throwing error by TS compiler -
[Angular] Identifier 'required' is not defined. '__type' does not contain such a member

@Mallesh25nayak
Copy link

i tried same thing bu tnot getting out put

@Danielapariona
Copy link

Danielapariona commented Sep 10, 2018

Example
try to add "?" after "price".
"<div *ngIf="price?.errors.required">Price is required."

@Mallesh25nayak
Copy link

First Name is required.
First Name should be minimum 3 character.
First Name doesn't Match pattern.
i tried this

@Mallesh25nayak
Copy link

<div *ngIf="firstName.errors.required">First Name is required. <div *ngIf="firstName.errors.minlength">First Name should be minimum 3 character.</div> <div *ngIf="firstName.errors.pattern">First Name doesn't Match pattern.</div> </div> i tried this

@danbete
Copy link

danbete commented Sep 12, 2018

this will solve the issue:
just add casting to bool using !! :
<div class="error" *ngIf="!!fullname.errors?.required && !!fullname.dirty">
Passenger name is required.

@heenamanglani
Copy link

heenamanglani commented Sep 27, 2018

Example
try to add "?" after "price".
"<div *ngIf="price?.errors.required">Price is required."

I solved this issue adding ? before parameter and not after errors? But can you please elaborate it and try to explain how this issue is solved just by moving the question mark? @Danielapariona

@suprcodr
Copy link

this will solve the issue:
just add casting to bool using !! :

Passenger name is required.

This solved my issue! thanks!

@EeeEui
Copy link

EeeEui commented Dec 6, 2018

Example
try to add "?" after "price".
"<div *ngIf="price?.errors.required">Price is required."

It worked for me. Could you please tell me why should add "?"

@MianMahroz
Copy link

this also solve my issue just add ?

@medeirosjoaquim
Copy link

But neither casting as boolean or using the ?(may not have) doesn't solve the fact that the intelliSense can't find the properties of errors.

@FeehGb
Copy link

FeehGb commented Feb 23, 2019

try price['errors']['required']

@danbete
Copy link

danbete commented Feb 24, 2019

@EeeEui the ? (safe navigation operator) will take a variable even if it's might be invalid and deal with the exception internally (invalid = false in this case).

@binDongKim
Copy link

? didn't work for me. !! only worked in my case.

@codermahadi
Copy link

#? didn't work f. !! only worked in angular 7+.

<div class="alert alert-danger" *ngIf="username.touched && username.invalid" class="alert alert-danger">
            <div *ngIf="!!username.errors.required">Username is required</div>
            <div *ngIf="!!username.errors.minlength">Username Should be minimum {{ !!username.errors.minlength.requiredLength }} Charecters.</div>
          </div>

@mhSangar
Copy link

It now seems like a problem with VSCode, because the error is still highlighted in my editor, but Angular does not detect it as so. What I did to resolve this for VSCode is check the error on the TypeScript side, with a function.

HTML

html

TypeScript

ts

@sathishiva
Copy link

sathishiva commented Jul 22, 2019

This worked for me and it looks cleaner.
<div class="error" *ngIf="fullname.hasError('required')">
this way is useful even if we handle custom validation as well such as fullname.hasError('invalidName')

@dhair-seva
Copy link

I had the same error. Doing *ngIf="formGroup?.get('fullName').hasError('required')" worked for me

@smallprogram
Copy link

It now seems like a problem with VSCode, because the error is still highlighted in my editor, but Angular does not detect it as so. What I did to resolve this for VSCode is check the error on the TypeScript side, with a function.

HTML

html

TypeScript

ts

Big Brother, this is too much trouble for you.

@prodplus
Copy link

prodplus commented Dec 6, 2019

Do *ngIf="yourForm.get('yourfield').hasError('required')"

@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 Feb 18, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests