First of all, there are a lot of mistakes in your code, let's enumerate it:
1 - Since your custom validator validDelarName is async, it must go as the 3rd. parameter, like this:
name: [
'',
[
Validators.required,
Validators.maxLength(128),
Validators.pattern('[a-zA-Z0-9\\s\\-\\,\\.\\&\\(\\)]+')
],
(control: AbstractControl) => this.validDelarName // Fix later
]
2 - You have to pass the control or the context to your custom validator, like below:
(control: AbstractControl) => this.validDelarName(control)
or if you prefer:
this.validDelarName.bind(this)
3 - The validDelarName's signature is wrong, it must be like this:
validDelarName(control: AbstractControl) { ... }
4 - The async validator waits or a Promise or an Observable and you're returning only null | errorObj inside the forEach, which one isn't doing anything.
Solution:
Instead of subscribe you could use map operator (or if you want you can use a Promise) and let Angular do his job.
To search for a specific value in your array I'd suggest you to use Array#some. It'll returns true if it finds the typed text in the array and will automatically stop the loop, otherwise it'll return false.
Based on this, you can return the error object or null, like this:
validDelarName(control: AbstractControl) {
return this.dealer.getviewdealer().map(data => {
const hasItem: boolean = data.some(item => control.value === item['dealername']);
return hasItem ? { valid: true } : null;
});
}
5 - As you can see above, you don't need to iterate over all the keys of data object (as you're doing) since you only want to compare the dealername.
6 (Minor mistake) - You have a typo in retrun null;, should be return null; :).
7 (Credits to @yurzui) - return {valid:true;} should be return { valid: true };
Tips:
1 - The Validators.compose isn't needed, you can just pass an array, or if it's a single validator, the validator itself, in both parameters (2nd and 3rd).
2 - Validators.pattern accepts a RegExp, so you can use it.
Why? Instead of escaping the symbols with double slashes, you can escape only a single slash and IMO, it's more readable.
Sample:
Validators.pattern(/^[a-zA-Z0-9\s-\,\.&\(\)]+$/)
Also, note that not all symbols here needs to be escaped (I take out the unneeded escaping).
FULL DEMO