使用Aurelia验证表

时间:2017-03-28 17:12:33

标签: aurelia aurelia-validation

我有一个带有输入框的行/列表,如下所示: Input table

使用此代码(简化):

<table>
  <thead>
    <!-- Column headings -->
    <tr>
      <th repeat.for="column of columns">
        ${column.display}
      </th>
    </tr>
  </thead>

  <tbody>
    <!-- Display all records -->
    <tr repeat.for="record of records">

      <!-- Show all column data for each record -->
      <td repeat.for="column of columns">
        <input type="text" value.bind="record[column.bind] & validate">
      </td>

    </tr>
  </tbody>
</table>

我创建了一组这样的规则:

this.rules = ValidationRules
  .ensure("gs_mark").required().minLength(1).maxLength(3)
  .ensure("gs_min").required().minLength(1).maxLength(3)
  .ensure("gs_max").required().minLength(1).maxLength(3)
  .ensure("gs_description").required().minLength(2).maxLength(20)
  .rules;

现在我需要将这些规则应用于记录,以便我可以验证所有输入框。

我该怎么做?我应该.map()记录并在每个记录上this.validationCtrl.addObject(this.record, this.rules);吗?我可以以某种方式设置每种控件类型的绑定验证,而无需将其应用于每个记录吗?我是以错误的方式接近这个吗?

1 个答案:

答案 0 :(得分:1)

最后,似乎让它工作的方法是遍历记录并将规则应用于每一行。

this.records.map((record) => {
  if ((typeof this.rules == 'object') && (this.rules.length)) {
    this.validationCtrl.removeObject(record);
    this.validationCtrl.addObject(record, this.rules);
  }
});

像魅力一样!我有点担心这对于非常大的表会占用多少内存,但至少它会起作用!

enter image description here