0

source code is here. The part I'm having trouble with is this:

export function forbiddenNameValidator(nameRe: RegExp): ValidatorFn {
  return (control: AbstractControl): ValidationErrors | null => {
    const forbidden = nameRe.test(control.value);
    return forbidden ? {forbiddenName: {value: control.value}} : null;
  };
}

The part I don't understand is the return part. We're returning a function within a function, and it confused me a lot. I've looked around a bit but haven't been able to find any references to this topic. can you help me?

  • Well, a function can return anything. It can return `undefined`, or a number, a string... or even another function. But then you have to call this new function : `const returnedFunction = forbiddenNameValidator("foo"); const finalResult = returnedFunction("bar");` or in short : `const finalResult = forbiddenNameValidator("foo")("bar")` – Jeremy Thille Feb 02 '22 at 07:28
  • but here we always send one argument: https://stackblitz.com/run?file=src%2Fapp%2Fshared%2Fforbidden-name.directive.ts – furki akça Feb 02 '22 at 07:39
  • The link redirects to the home page – Jeremy Thille Feb 02 '22 at 07:41
  • https://stackblitz.com/edit/angular-9bo4pq?file=src%2Fapp%2Fshared%2Fforbidden-name.directive.ts – furki akça Feb 02 '22 at 07:43
  • 1
    @furkiakça `forbiddenNameValidator(new RegExp(this.forbiddenName, 'i'))(control)` is most definitely passing two arguments in a curried fashion. To simplify it's: `forbiddenNameValidator(one)(two)` – VLAZ Feb 02 '22 at 07:45
  • wow i got it, thank you – furki akça Feb 02 '22 at 07:51

0 Answers0