繁体   English   中英

Typescript class 属性的类验证器

[英]Typescript Class-Validator for property of class

我试图将类验证器装饰器用于验证过程。 所以,我在我的示例项目中实现了。 但是,它不起作用。 示例项目正在尝试使用用户输入创建示例项目,并且我正在尝试检查这些输入(例如我的代码中的示例,我正在尝试使用该类验证器验证标题包含。但是,装饰器在值的集合之前执行输入元素。因此,标题一直是空的,并且验证失败。

我错过了什么? 如何使用该库从输入中获取有效到即将到来的值?

我的 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");
  }
});

索引.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>

您将 validate 方法放在 app.ts 脚本中。 它在脚本执行时调用,因此在页面加载时调用。 如果我理解你,你想在用户在 titleInputElement 中输入另一个字符时进行验证。 您可以通过

@Contains('hello')
titleValue: string;

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

值得一提的是,您需要验证titleInputElement: HTMLElement之外的其他属性。 正如文档所说:

@Contains(seed: string) 检查字符串是否包含种子。

所以它必须是字符串属性(在我的例子中是titleValue)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM