I have a component, form and business validators behind them. Additionally, have html which shows how the form is rendered.
The thing is, what if someone wants to utilize the formbuilder with same validators, however a totally different html rendering?
How to separate the form below, so formcontrols and validators, are separated and not strongly coupled to the html format?
Can we Export as a class, or Input/Output, or ControlValueAccessor?
Currently this is one component. Note: will need ability to handle more complex validators (eg, City/State or Zip code required, one or the other)
CustomerInfo.component:
Typescript
constructor(
private formBuilder: FormBuilder,
private cdr: ChangeDetectorRef
) {
this.addressform = this.formBuilder.group({
'firstName': [null, [Validators.maxLength(32)]],
'lastName': [null, [Validators.maxLength(32)]],
'phone': [null, [Validators.maxLength(32)]],
'streetName': [null, [Validators.required, Validators.maxLength(64)]],
'streetType': [null, [Validators.maxLength(8)]],
'city': [null, [Validators.maxLength(32)]],
'state': [null, [Validators.maxLength(16)]],
'postalCode': [null, [Validators.maxLength(16)]],
}, { validator: atLeastOneLocationRequired })
// City,State or PostalCode Required
export function atLeastOneLocationRequired(group : FormGroup) : {[s:string ]: boolean} {
var cityCtrl = group.controls.city;
var stateCtrl = group.controls.state;
var postalCodeCtrl = group.controls.postalCode;
if (cityCtrl != undefined && stateCtrl != undefined && postalCodeCtrl != undefined)
if (!(((cityCtrl.value && cityCtrl.value.length) && (stateCtrl.value && stateCtrl.value.length)) || (postalCodeCtrl.value && postalCodeCtrl.value.length)))
return { invalid: true };
}
HTML The formbuilder will require a different html format later.
<div class = "propertysitusaddress">
<form [formGroup]="addressform">
<div class = "propertysiteaddresscontainer column">
<div class = "row title">
Address
</div>
<div class = "row">
<app-input-textboxc formControlName = "firstName"> </app-input-textbox>
<app-input-textbox formControlName = "lastName"> </app-input-textbox>
<app-input-textbox formControlName = "phone"> </app-input-textbox>
</div>
<div class = "row">
<app-input-textbox formControlName = "streetName"> </app-input-textbox>
<app-input-textbox formControlName = "city"> </app-input-textbox>
<app-input-textbox formControlName = "state"> </app-input-textbox>
</div>
<div class = "row">
<app-input-textbox formControlName = "postalCode"> </app-input-textbox>
</div>
</div>
Its often mentioned to separate smart and presentational components in Angular.