I am building a form with reactive form, but i have some nested group inside group, and for that nested group i have only validation required on input. The problem I have is that inside that nested group, i have to have validation that if user enters in one input something, others inputs in that group are not required anymore.
This is what i have for now, i have tried to add Validators.required instead RequiredValidatonOnlyOne(). Is it possible to add some custom validation that will validate that user has enters something in just one input
this.formGroup = this.formBuilder.group({
lastName: [{ value: '', disabled: false }, [Validators.maxLength(50), Validators.required]],
phones: this.formBuilder.group({
phone: [{ value: '', disabled: false }],
phone2: [{ value: '', disabled: false }],
mobile: [{ value: '', disabled: false }],
}, this.RequiredValidatonOnlyOne())
});
private RequiredValidatonOnlyOne(){
return (controlGroup) => {
const controls = controlGroup.controls;
if (controls) {
const theOne = Object.keys(controls).find(key => controls[key].valid);
if (!theOne) {
return {
atLeastOneRequired: {
text: 'At least one should be selected'
}
};
}
}
return null;
};
}
RequiredValidatonOnlyOne()so we can help? it is custom validator and probably something wrong with it. - Nikola Stekovic