'Why should I add '?' in the validation of reactive form?
I am creating a reactive form in angular 13. When doing validation I am only able to make it work like this:
<div class="form-group">
<label for="firstname">First Name : </label>
<input type="text" class="form-control" id="firstname" name="firstname" formControlName="firstname" placeholder="Enter First Name">
<div *ngIf= "!firstname?.valid && (firstname?.dirty || firstname?.touched)">
<div [hidden]= "!firstname?.errors?.['required']"> First Name is required {{!firstname?.errors?.['required']}}</div>
<div [hidden]= "!firstname?.errors?.['minlength']"> Min Length is 10 {{!firstname?.errors?.['minlength']}} </div>
</div>
</div>
To validate the firstname. Why should I add the
?
character after firstname
and errors
?
Noting that in the component I have a get method like that:
get firstname() {
return this.contactForm.get('firstname');
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|