Hello,
I created a custom Typeahead field in blockly which shows suggestion(dropdown) as you type in the field.
I use an API call to backend to get the results and update the dropdown whenever user type something in the field.
Now i want to set Validator on the field to check if the entered value matches the option in the dropdown and show error if it doesnt and set to empty.
I made the API call inside setValidator and then check if entered value matches, the code for which is below:-
Blockly.Extensions.register('dynamic_UCUM', function (block) {
var ucum_field = this.getField('UCUM_suggestions');
// Empty text field should not display any suggestion
var ucum_validator = (newValue) => {
if (!newValue) {
current.updateMenuGenerator(ucum_field, []);
}
else {
current.restService.getUCUM(newValue).subscribe(result => {
var names = new Array();
var ucum_suggestions: any[] = result[3];
ucum_suggestions.forEach(element => {
names.push(element[0] + " " + element[1])
});
current.updateMenuGenerator(ucum_field, names);
});
}
if (!(ucum_field as any).menuGenerator.includes(newValue)) {
return null;
}
return newValue;
}
ucum_field.setValidator(ucum_validator);
});
The problem here is that the if block check of includes gets executed before API response and hence the validation is not proper.
I also used async await but it is not working.
Can anyone suggest me how to tackle this problem so that the if check gets executed after the API response in setValidator Or alternate way to do this