Если поле электронной почты не пустое, то поле для пересылки электронной почты должно быть 'обязательным'. Для этого мне нужно добавить условные базовые валидаторы в поле для пересылки электронной почты, я пробовал ниже код,
HTML
<div class="row">
<div class="form-group col-xs-6">
<label class="control-label">Email Address:</label>
<input type="text" class="form-control" (blur)="reEnterEmail()" [ngClass]="{ 'quote-has-error' : startPageForm.controls['email'].invalid && startPageForm.controls['email'].touched}"
formControlName="email">
<label class="text-danger" *ngIf="startPageForm.controls['email'].hasError('pattern') && startPageForm.controls['email'].touched">Email is invalid</label>
</div>
<div class="form-group col-xs-6">
<label class="control-label">Re-enter Email Address :</label>
<input type="text" class="form-control" (blur)= "checkReEmail()" [ngClass]="{ 'quote-has-error' : startPageForm.controls['reEmail'].invalid && startPageForm.controls['reEmail'].touched}"
formControlName="reEmail" id="reEmailId">
<label class="text-danger" *ngIf="startPageForm.controls['reEmail'].touched && message == 'false' &&
this.startPageForm.controls.email.value != this.startPageForm.controls.reEmail.value" >Email is invalid</label>
</div>
</div>
Компонент
this.startPageForm = new FormGroup({
firstName : new FormControl( '', [Validators.required]),
middleName : new FormControl( '', [] ),
lastName : new FormControl( '', [Validators.required] ),
suffix : new FormControl( '', [] ),
dateOfBirth : new FormControl( this.dateOfBirth, [Validators.required, DateValidator, AgeValidator] ),
gender : new FormControl( this.genderEnumConstants[0].value, [] ),
maritalStatus : new FormControl( this.maritalStatusEnumConstants[0].value, [] ),
aprtUntNumber : new FormControl( '', [] ),
address : new FormControl( '', [] ),
city : new FormControl( '', [Validators.required] ),
stateId : new FormControl( '', [Validators.required] ),
zipcode : new FormControl( '', [Validators.required, Validators.maxLength(5)] ),
isMilitrayAddress : new FormControl( false, [] ),
mobileNo : new FormControl( '', [Validators.required] ),
homeNo : new FormControl( '', [] ),
email : new FormControl( '', [ Validators.pattern("^[a-z0-9._%+-]+@[a-z0-9.-]+\\.[a-z]{2,4}$")] ),
reEmail : new FormControl( '', [] ),
notificationMethod : new FormControl( this.notificationMethodEnumConstants[0].value, [] ),
policyTerm : new FormControl( this.policyTermEnumConstants[0].value, [] ),
effectiveDate : new FormControl( effectiveDate, [DateValidator, MinDateValidator] ),
});
reEnterEmail() {
this.startPageForm.controls['reEmail'].setValidators(this.setRequired());
}
setRequired() {
if(this.startPageForm.controls.email.value != null && this.startPageForm.controls.email.value != '') {
return [Validators.required];
} else {
return [];
}
}
Вкл. email
поле (blur)="reEnterEmail()"
событие Я устанавливаю валидаторы в поле reEmail
.Но я получил следующее ошибка .
Uncaught ReferenceError: Validators is not defined(…)