Skip to content

Instantly share code, notes, and snippets.

@Spittal
Created May 13, 2016 18:35
Show Gist options
  • Save Spittal/ce2e7dee73d41272117ef903829e47cc to your computer and use it in GitHub Desktop.
Save Spittal/ce2e7dee73d41272117ef903829e47cc to your computer and use it in GitHub Desktop.
import { Injectable } from '@angular/core';
import {
Validators,
Control,
ControlGroup
} from '@angular/common';
import { ValidationHelper } from
'../../../../common/helpers/validation.helper';
@Injectable()
export class HnyShippingAddressFormBuilder {
public static buildForm(address?): ControlGroup {
if (!address) { address = {}; }
return new ControlGroup({
firstname: this.firstname(address.firstname),
lastname: this.lastname(address.lastname),
email: this.email(address.email),
telephone: this.telephone(address.telephone),
address1: this.address1(address.address1),
country_id: this.country_id(address.country_id),
city: this.city(address.city),
region: this.region(address.region),
postcode: this.postcode(address.postcode)
});
}
/**
* Inidividual values
*/
private static firstname(initValue?): Control {
return new Control(
initValue || '',
Validators.required
);
}
private static lastname(initValue?): Control {
return new Control(
initValue || '',
Validators.required
);
}
private static email(initValue?): Control {
return new Control(
initValue || '',
Validators.compose([
Validators.required,
ValidationHelper.emailValidator
])
);
}
private static telephone(initValue?): Control {
return new Control(
initValue || '',
Validators.compose([
Validators.required,
ValidationHelper.phoneValidator
])
);
}
private static address1(initValue?): Control {
return new Control(
initValue || '',
Validators.required
);
}
private static country_id(initValue?): Control {
return new Control(
initValue || '',
Validators.required
);
}
private static city(initValue?): Control {
return new Control(
initValue || '',
Validators.required
);
}
private static region(initValue?): Control {
return new Control(
initValue || '',
Validators.required
);
}
private static postcode(initValue?): Control {
return new Control(
initValue || '',
Validators.required
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment