How can I run custom validation on this component in the FormBuilder?
For example, when a user selects an item from the dropdown onClickItem(item)
how could run a validation function on the selected item
from the main form component?
FormBuilder in main form component:
this.itemForm = this.fb.group({
name: ['', [Validators.required, Validators.minLength(3)]],
description: ['', [Validators.required, Validators.minLength(10)]],
itemName: [] // run validation on this component
});
Then I could use a validator on itemName like this: itemName: [, MyValidator.itemNotUsed],
Template:
<app-dropdown-select formControlName="itemName"
[dropdownItems]="items">
</app-dropdown-select>
Dropdown-Select Component:
@Component({
selector: 'app-dropdown-select',
templateUrl: './dropdown-select.component.html',
styleUrls: ['./dropdown-select.component.scss'],
providers: [
{
provide: NG_VALUE_ACCESSOR,
useExisting: forwardRef(() => DropdownSelectComponent),
multi: true
}
]
})
export class DropdownSelectComponent implements ControlValueAccessor {
@Input() combinedInput: boolean;
@Input() dropdownItems: DropdownItem[];
_selectedItem: DropdownItem;
showList: boolean;
buttonIcon: string;
propagateChange = (_: any) => {};
set selectedItem(value) {
this._selectedItem = value;
this.propagateChange(this._selectedItem);
}
get selectedItem() {
return this._selectedItem;
}
constructor(private el: ElementRef) { }
OnInit() {
this.buttonIcon = BUTTON_ICON_INACTIVE;
this.selectedItem = this.dropdownItems[0];
console.log(this.dropdownItems);
}
onClick() {
this.toggleShowList();
}
toggleShowList() {
this.showList = !this.showList;
if (!this.showList) {
this.buttonIcon = BUTTON_ICON_INACTIVE;
} else {
this.buttonIcon = BUTTON_ICON_ACTIVE;
}
}
onClickItem(item) {
this.showList = false;
this.selectedItem = item;
this.propagateChange(this.selectedItem);
}
writeValue(value: any) {
if (value !== undefined) {
this.selectedItem = value;
}
}
registerOnChange(fn) {
console.log('register change');
this.propagateChange = fn;
}
registerOnTouched() {}
}