0

This may be an easy question/answer, but I can't seem to wrap my head around it: I am validating fields with a function using AbstractControl:

errorVar: boolean = false

function(c: AbstractControl): {[key: string]: string } | null {
 // validation if 'test' is true or not goes here
 if(test) {
  let errorMessageText: "test"
  return {'errorText': errorMessageText};
 }
return null;
}

Besides the errorText I want the function to also set the variable errorVar to true and to false if the function returns null.

2
  • And what is your problem? Commented Nov 14, 2018 at 9:15
  • If I try to set the variable inside the function it comes back as "undefined is not an object. So I'm fairly new to this and can't figure out why. So my idea was to have the function return the variable Commented Nov 14, 2018 at 10:47

2 Answers 2

1

You can do something like this:

  errorVar: boolean = false

  function(c: AbstractControl): { [key: string]: string } | null {
    // validation if 'test' is true or not goes here
    if (test) {
      this.errorVar = true;
      let errorMessageText:
        return { 'errorText': errorMessageText };
    }

    this.errorVar = false;
    return null;
  }
Sign up to request clarification or add additional context in comments.

3 Comments

Nope, tried that first of course. It comes back "undefined is not an object"
Is errorVar defined within the context of a class?
yes, it is - it's a reactive form and the variable definition and function is inside the export class
0

I think there are several problems

  • your errorVar should be declared using var or let so you can set it in the function via a closure
  • your errorMessageText variable must be set using = and not :

like this:

var errorVar: boolean = false;

function(c: AbstractControl): {[key: string]: string } | null {
    // validation if 'test' is true or not goes here
    if(test) {
        errorVar = true;
        let errorMessageText: string ="test";
        return {'errorText': errorMessageText};
    }
    errorVar = false;
    return null;
}

5 Comments

Thanks for your answer, but that's not why it's not working. The errorVar: boolean = false was meant as a pointer that I have declared the variable earlier in the class; the function is a representation what I want to achieve, not a functioning piece of code. Not that I can explain it, but rest assured, I have about 20 other declarations that work just fine this way. ...and to your second point, TypeScript let's you define the variable type by using a colon after the name.
Well, I tested my code on TypeScript playground. let errorMessageText: "test" did not work (errorMessageTextwas undefined), but let errorMessageText= "test" did.
Concerning the "pointer" thing, what you can do is pass an object { errorMessageText:"" } as parameter, and modify its property inside the function.
Of course it didn't, because as I said before, you define the type of the variable with the colon ":" and write it with the equal sign "=". So in this example I am setting errorMessageText as type string.... but all that is beside the point, because the function properly returns errorMessageText, what I need is that the function returns a SECOND variable!
Ok, this was a typo error. Could it be that you have a problem of this binding that prevents you from settings errorVar (if it's declared in a class)? In that case, you could bind explicitly by passing myFunction.bind(myInstance) instead of myFunction. Anyway, good luck for your problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.