|
@@ -3,6 +3,7 @@ import { FormBuilder, FormControl, FormGroup, FormArray, FormGroupName, Abstract
|
|
|
import { SuperForm } from 'angular-super-validator';
|
|
|
import { buildFormGroupFunctionFactory } from './services/_formdata-utils';
|
|
|
import { cloneDeep } from 'lodash/fp';
|
|
|
+import { unwrapResolvedMetadata } from '@angular/compiler';
|
|
|
|
|
|
export interface DynarowContext {
|
|
|
control: AbstractControl;
|
|
@@ -181,23 +182,47 @@ export class DynaformComponent implements OnInit {
|
|
|
|
|
|
}
|
|
|
|
|
|
+ addRCMemberAllowed(repeatingContainerName: string): boolean {
|
|
|
+ const rcMeta = this.formMetaData[repeatingContainerName];
|
|
|
+ return typeof rcMeta.maxRepeat === 'number' && rcMeta.maxRepeat > rcMeta.meta.length;
|
|
|
+ }
|
|
|
+
|
|
|
// Maybe move the next two funtions to _formdata-utils.ts ?
|
|
|
addRCMember(repeatingContainerName: string): void {
|
|
|
- // (1) Add metadata for new container member
|
|
|
+ // (1) Check that we vcan still add controls
|
|
|
+ if (!this.addRCMemberAllowed(repeatingContainerName)) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ // (2) Add metadata for new container member
|
|
|
const rcMeta = this.formMetaData[repeatingContainerName];
|
|
|
const containerTemplate = cloneDeep(rcMeta.__containerTemplate);
|
|
|
const i = this.formMetaData[repeatingContainerName].meta.length;
|
|
|
containerTemplate.name = `group${i+1}`;
|
|
|
rcMeta.meta.push(containerTemplate);
|
|
|
-
|
|
|
- // (2) Add FormGroup for new container member
|
|
|
+ // (3) Add FormGroup for new container member
|
|
|
const buildFormGroup = buildFormGroupFunctionFactory(new FormBuilder());
|
|
|
const newFormGroup = buildFormGroup(containerTemplate.meta);
|
|
|
(this.formGroup.get(repeatingContainerName) as FormArray).push(newFormGroup);
|
|
|
}
|
|
|
|
|
|
- deleteRCMember(repeatingContainerName: string, index: number): void {
|
|
|
+ deleteRCMemberAllowed(repeatingContainerName: string): boolean {
|
|
|
+ const rcMeta = this.formMetaData[repeatingContainerName];
|
|
|
+ return typeof rcMeta.minRepeat === 'number' && rcMeta.minRepeat < rcMeta.meta.length;
|
|
|
+ }
|
|
|
|
|
|
+ deleteRCMember(repeatingContainerName: string, index: number): void {
|
|
|
+ // (1) Check that we can still delete controls
|
|
|
+ if (!this.deleteRCMemberAllowed(repeatingContainerName)) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ // (2) Delete from the metadata, and rename the groups
|
|
|
+ const rcMeta = this.formMetaData[repeatingContainerName];
|
|
|
+ const metaArr = [ ...rcMeta.meta ];
|
|
|
+ const newMetaArr = [ ...metaArr.slice(0, index), ...metaArr.slice(index + 1) ]
|
|
|
+ .map((m, i) => { m.name = `group${i+1}`; return m; });
|
|
|
+ rcMeta.meta = newMetaArr;
|
|
|
+ // (3) Delete the corresponding FormGroup from the FormArray
|
|
|
+ (this.formGroup.get(repeatingContainerName) as FormArray).removeAt(index);
|
|
|
}
|
|
|
|
|
|
getValidationFailureMessage(control: FormControl, meta: StringMap<any>) {
|