gpt4 book ai didi

Aurelia 验证不适用于对象属性

转载 作者:行者123 更新时间:2023-12-01 00:28:54 25 4
gpt4 key购买 nike

我无法让 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'),验证有效并在表单上呈现(但数据未绑定(bind)到此类中数据的正确部分.

如何让 Aurelia Validate 识别 calendar.cal_name_orig?

最佳答案

解决方案是将 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);

关于Aurelia 验证不适用于对象属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40250698/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com