Aurelia验证不使用对象属性

时间:2016-10-25 22:21:01

标签: aurelia-validation

我无法让Aurelia-Validate处理日历记录中的字段。

calendar.html (摘录)

<form>
  <div class="form-group">
    <label class="control-label" for="cal_name_orig">Calendar name</label>
    <input type="text" class="form-control" id="cal_name_orig" value.bind="calendar.cal_name_orig & validate">
  </div>
  <div class="form-group">
    <label class="control-label" for="cal_name_tran">Translated name</label>
    <input type="text" class="form-control" id="cal_name_tran" value.bind="calendar.cal_name_tran & validate">
  </div>
</form>

calendars.js (简体):

import { inject, NewInstance } from 'aurelia-dependency-injection';
import { ValidationController, ValidationRules } from 'aurelia-validation';
import { BootstrapFormRenderer } from '../../common/bootstrap-form-renderer';

@inject(NewInstance.of(ValidationController))
export class CalendarForm {
  controller = null;

  constructor(controller) {
    this.controller = controller;
    this.controller.addRenderer(new BootstrapFormRenderer());
    this.calendar = {
      cal_name_orig = "",
      cal_name_tran = ""
    }
  }

  validateCalendar() {
    let v = this.controller.validate();
    console.log(v);
  }

}

ValidationRules
  .ensure('calendar.cal_name_orig').required().minLength(5).maxLength(20)
  .ensure('calendar.cal_name_tran').minLength(5).maxLength(20)
  .on(CalendarForm);

如果我将value.bind="calendar.cal_name_orig & validate"更改为value.bind="cal_name_orig & validate".ensure('calendar.cal_name_orig')更改为.ensure('cal_name_orig'),则验证将在表单上生效并呈现(但数据不会绑定到正确的部分)本课程中的数据。

如何让Aurelia Validate识别calendar.cal_name_orig?

1 个答案:

答案 0 :(得分:4)

解决方案是将ValidationRules移动到类constructor()中,并将最后一行更改为on(this.calendar)。我还需要从每个保证规则中删除calendar.

ValidationRules
  .ensure('cal_name_orig').required().minLength(5).maxLength(20)
  .ensure('cal_name_tran').required().minLength(5).maxLength(20)
  .on(this.calendar);