This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<h2>My form</h2> | |
<form [formGroup]="myForm" | |
(ngSubmit)="onSubmit()"> | |
<div> | |
Make email mandatory <input formControlName="myCheckbox" | |
type="checkbox"> | |
</div> | |
<div> | |
Email: <input formControlName="myEmailField" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ngOnInit() { | |
this.myForm = this.fb.group({ | |
myCheckbox: [''], | |
myEmailField: ['', [ | |
Validators.maxLength(250), | |
Validators.minLength(5), | |
Validators.pattern(/.+@.+\..+/) | |
]] | |
}); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
this.myForm.get('myCheckbox').valueChanges | |
.subscribe(value => { | |
if(value) { | |
this.myForm.get('myEmailField').setValidators(Validators.required) | |
} else { | |
this.myForm.get('myEmailField').clearValidators(); | |
} | |
} | |
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
this.myForm.get('myCheckbox').valueChanges | |
.subscribe(value => { | |
if(value) { | |
this.myForm.get('myEmailField').setValidators(Validators.required) | |
} else { | |
this.myForm.get('myEmailField').clearValidators(); | |
} | |
} | |
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private emailValidators = [ | |
Validators.maxLength(250), | |
Validators.minLength(5), | |
Validators.pattern(/.+@.+\..+/) | |
]; | |
ngOnInit() { | |
this.myForm = this.fb.group({ | |
myCheckbox: [''], | |
myEmailField: ['', this.emailValidators] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
this.myForm.get('myCheckbox').valueChanges | |
.subscribe(value => { | |
if(value) { | |
this.myForm.get('myEmailField').setValidators(this.emailValidators.concat(Validators.required)) | |
} else { | |
this.myForm.get('myEmailField').setValidators(this.emailValidators); | |
} | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
this.myForm = this.fb.group({ | |
myCheckbox: [''], | |
myEmailField: ['', [ | |
Validators.maxLength(250), | |
Validators.minLength(5), | |
Validators.pattern(/.+@.+\..+/) | |
]] | |
}, { | |
validators: [emailConditionallyRequiredValidator] // <----- | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
this.myForm = this.fb.group({ | |
myCheckbox: [''], | |
myEmailField: ['', [ | |
Validators.maxLength(250), | |
Validators.minLength(5), | |
Validators.pattern(/.+@.+\..+/) | |
]] | |
}, { | |
validators: [emailConditionallyRequiredValidator] // <----- | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function emailConditionallyRequiredValidator(formGroup: FormGroup) { | |
if (formGroup.value.myCheckbox) { | |
return Validators.required(formGroup.get('myEmailField')) ? { | |
myEmailFieldConditionallyRequired: true, | |
} : null; | |
} | |
return null; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
this.myForm = this.fb.group({ | |
myCheckbox: [''], | |
myEmailField: ['', [ | |
Validators.maxLength(250), | |
Validators.minLength(5), | |
Validators.pattern(/.+@.+\..+/), | |
emailConditionallyRequiredValidator | |
]] | |
}) |
OlderNewer