rxweb.io icon indicating copy to clipboard operation
rxweb.io copied to clipboard

I am not able to set the custom validator for formarray.

Open ghost opened this issue 5 years ago • 2 comments

ghost avatar Feb 24 '20 19:02 ghost

@ram54118 Please share the code snippet.

ajayojha avatar Feb 25 '20 21:02 ajayojha

export class TestInfo{ @prop() id: string;

@required() @email({ message: 'Invalid email' }) email: string;

@required() type: string;

@prop() primary: boolean;

@prop() changeType: string;

@prop() verified: boolean;

@prop() verifiedDate: string;

@prop() allowEdit: string; @prop() allowDelete: string; @prop() markedForDeletion: string; }

export class TestInfoDetails { // I want to add custom validator(atleastOnePrimary below validator) for this formArray. @propArray(TestInfo) items: Array<TestInfo>; }

// Validator: public static atleastOnePrimary() { const validator: ValidatorFn = (formArray: FormArray) => { const primaryList = formArray.controls.filter( (group: FormGroup) => group.controls.primary.value === true && group.controls.markedForDeletion.value !== true );

  return primaryList && primaryList.length === 1 ? null : { onePrimary: false };
};

return validator;

}

ghost avatar Feb 28 '20 09:02 ghost