Currently, I have an Angular 5 project with a method called createForm. This method creates a form which varies based on the flight inventory code. If the flight inventory code is TAG_ON, then the form excludes the messagePrefixSMSValidator and sets the message form control to empty. Here is my code:
createForm() {
this.formGroup = this.fb.group(
{
defaultTemplate: [this.defaultInitialValue],
language: [null, Validators.required],
message: [ this.messagePrefix ? this.messagePrefix:'', [Validators.required]],
longUrl: ['']
},
{
validator: [
hasUrlTagValidator(TemplatesService.urlTag),
messagePrefixSMSValidator(this.messagePrefix? this.messagePrefix: null, 'message')
]
}
);
if(this.flight.inventory.code === FlightInventoryCode.TAG_ON) {
this.formGroup = this.fb.group(
{
defaultTemplate: [this.defaultInitialValue],
language: [null, Validators.required],
message: [ '', [Validators.required]],
longUrl: ['']
},
{
validator: [
hasUrlTagValidator(TemplatesService.urlTag),
]
}
);
}
}
Is there a simpler way to rewrite my code?
Any help would be greatly appreciated. Thank you.