rxweb / rxweb.io

rxweb.io documentation
MIT License
18 stars 23 forks source link

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

Open ghost opened 4 years ago

ajayojha commented 4 years ago

@ram54118 Please share the code snippet.

ghost commented 4 years ago

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; }

// 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;

}