0

I was trying to use the class-validator decorator library for the validation processes . So, I was implemented on my sample project. But, it is not working. The sample project is trying to create sample projects with user inputs and I am trying to check those inputs(example on my code I am trying to validate contains of title with that class-validator. But, decorators are executing before the set of value of input elements. So, the title looks empty all time, and validation fails.

What I am missing? How can I use that library for valid to upcoming value from inputs?

My app.ts = >


import { validate, Contains } from "class-validator";

class ProjectInput {
  templateElement: HTMLTemplateElement;
  hostElement: HTMLDivElement;
  formElement: HTMLFormElement;

  @Contains("hello")
  titleInputElement: HTMLInputElement;

  descriptionInputElement: HTMLInputElement;
  peopleInputElement: HTMLInputElement;

  constructor() {
    this.templateElement = <HTMLTemplateElement>(
      document.getElementById("project-input")!
    );
    this.hostElement = <HTMLDivElement>document.getElementById("app")!;
    const importedNode = document.importNode(
      this.templateElement.content,
      true
    );
    this.formElement = <HTMLFormElement>importedNode.firstElementChild;
    this.formElement.id = "user-input";

    this.titleInputElement = <HTMLInputElement>(
      this.formElement.querySelector("#title")
    );

    this.descriptionInputElement = <HTMLInputElement>(
      document.getElementById("description")
    );

    this.peopleInputElement = <HTMLInputElement>(
      document.getElementById("people")
    );
    this.configure();
    this.attach();
  }

  private submitHandler(event: Event) {
    event.preventDefault();
    console.log(this.titleInputElement.value);
  }

  private configure() {
    this.formElement.addEventListener("submit", this.submitHandler.bind(this));
  }
  private attach() {
    this.hostElement.insertAdjacentElement("afterbegin", this.formElement);
  }
}

const prjInputExample = new ProjectInput();
validate(prjInputExample).then((errors) => {
  // errors is an array of validation errors
  if (errors.length > 0) {
    console.log("validation failed. errors: ", errors);
  } else {
    console.log("validation succeed");
  }
});

index.html =>


<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>ProjectManager</title>
    <link rel="stylesheet" href="app.css" />
    <script src="bundles/bundle.js" defer></script>
  </head>
  <body>
    <template id="project-input">
      <form>
        <div class="form-control">
          <label for="title">Title</label>
          <input type="text" id="title" />
        </div>
        <div class="form-control">
          <label for="description">Description</label>
          <textarea id="description" rows="3"></textarea>
        </div>
        <div class="form-control">
          <label for="people">People</label>
          <input type="number" id="people" step="1" min="0" max="10" />
        </div>
        <button type="submit">ADD PROJECT</button>
      </form>
    </template>
    <template id="single-project">
      <li></li>
    </template>
    <template id="project-list">
      <section class="projects">
        <header>
          <h2></h2>
        </header>
        <ul></ul>
      </section>
    </template>
    <div id="app"></div>
  </body>
</html>

1 Answer 1

1

You placed validate method just inside app.ts script. It is invoked when the script is executed, so at the page load. If I understand you well you want to make validation when user type in another character into titleInputElement. You can achieve it via

@Contains('hello')
titleValue: string;

titleInputElement.addEventListener('input', event => {
    this.titleValue = this.titleInputElement.value;
    validate(this);
})

Worth to mention that you need to validate other property than titleInputElement: HTMLElement. As docs says:

@Contains(seed: string) Checks if the string contains the seed.

So it's gotta be string property (in my example it's titleValue)

Sign up to request clarification or add additional context in comments.

4 Comments

I want to make validation with using the class-validator library when the title contains a specific string as above example "hello". I tried to use your code but it didn't work for me or still, I am missing something.
I've edited my post. I've missed that titleInputElement type is not suitable with @Contains.
I am getting that error :Property 'value' does not exist on type 'EventTarget. If you could clone the project and if you could to try it from your side I would appreciate it . The repo's link
this.titleValue = this.titleInputElement.value; should do better

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.